Auto Scaling Concepts
What is Auto Scaling?
Auto Scaling automatically adjusts the number of EC2 instances based on demand. This ensures performance during traffic spikes and reduces costs during low-demand periods.
┌─────────────────────────────────────────────────────────────┐
│ AUTO SCALING - CONCEPT │
├─────────────────────────────────────────────────────────────┤
│ │
│ WITHOUT AUTO SCALING │
│ ───────────────── │
│ │
│ Capacity ▲ │
│ │ ┌─────────────────────────────────┐ │
│ │ │ Fixed capacity (10 instances) │ │
│ │ ───┴─────────────────────────────────┴─── │
│ │ /\ │
│ │ / \ Actual demand │
│ │ / \___________ │
│ │ / \ │
│ │ / \___ │
│ └─────────────────────────────► Time │
│ │
│ Problems: ❌ Over-provisioned at night = waste │
│ ❌ Under-provisioned at peaks = slow │
│ │
│ ──────────────────────────────────────────────────────── │
│ │
│ WITH AUTO SCALING │
│ ───────────────── │
│ │
│ Capacity ▲ │
│ │ ________ │
│ │ / \ Capacity adapts │
│ │ / \___________ │
│ │ / \ │
│ │ / \___ │
│ └─────────────────────────────────► Time │
│ │
│ Advantages: ✅ Capacity = Demand (always) │
│ ✅ Optimized costs │
│ ✅ Maintained performance │
│ │
└─────────────────────────────────────────────────────────────┘
Auto Scaling Components
Architecture
┌─────────────────────────────────────────────────────────────┐
│ AUTO SCALING COMPONENTS │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ LAUNCH TEMPLATE │ │
│ │ │ │
│ │ "How to create new instances" │ │
│ │ • AMI : ami-xxxxx │ │
│ │ • Type : t3.medium │ │
│ │ • Security Group : sg-web │ │
│ │ • User Data : startup script │ │
│ │ • Key Pair : my-key │ │
│ │ │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AUTO SCALING GROUP (ASG) │ │
│ │ │ │
│ │ "How many instances and where" │ │
│ │ • Min : 2 │ │
│ │ • Max : 10 │ │
│ │ • Desired : 4 │ │
│ │ • Subnets : subnet-1a, subnet-1b │ │
│ │ • Health Check : EC2 or ELB │ │
│ │ │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ SCALING POLICIES │ │
│ │ │ │
│ │ "When to add/remove instances" │ │
│ │ • CPU > 70% → +2 instances │ │
│ │ • CPU < 30% → -1 instance │ │
│ │ • Schedule : 8h → 6 instances, 20h → 2 instances │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Components Table
| Component | Role | Example |
|---|---|---|
| Launch Template | Defines instance config | AMI, type, SG, user data |
| ASG | Manages instance group | Min/Max/Desired, subnets |
| Scaling Policy | Scaling rules | CPU > 70% → scale out |
| CloudWatch Alarm | Triggers scaling | Metric alert |
Types of Scaling
1. Manual Scaling
Manually changing the number of instances.
# Change the desired number manually
aws autoscaling set-desired-capacity \
--auto-scaling-group-name my-asg \
--desired-capacity 5
Use case: Predictable events (planned Black Friday)
2. Scheduled Scaling
Scaling based on a predefined schedule.
┌─────────────────────────────────────────────────────────────┐
│ SCHEDULED SCALING │
├─────────────────────────────────────────────────────────────┤
│ │
│ Example: E-commerce site with predictable traffic │
│ │
│ Instances ▲ │
│ │ │
│ 10─┤ ┌───────────────┐ │
│ │ │ │ │
│ 6─┤ ┌────┘ └────┐ │
│ │ │ │ │
│ 2─┤─────┘ └───── │
│ │ │
│ └─────┬─────┬─────┬─────┬─────┬─────► Hour │
│ 6h 9h 12h 18h 22h │
│ │
│ Rules: │
│ • 6h → min:2, max:6 (before opening) │
│ • 9h → min:6, max:10 (peak hours) │
│ • 22h → min:2, max:4 (night) │
│ │
└─────────────────────────────────────────────────────────────┘
# Create a scheduled action
aws autoscaling put-scheduled-update-group-action \
--auto-scaling-group-name my-asg \
--scheduled-action-name scale-up-morning \
--recurrence "0 9 * * MON-FRI" \
--min-size 6 \
--max-size 10 \
--desired-capacity 6
3. Dynamic Scaling (Target Tracking)
The most recommended. AWS automatically maintains a target metric.
┌─────────────────────────────────────────────────────────────┐
│ TARGET TRACKING SCALING │
├─────────────────────────────────────────────────────────────┤
│ │
│ Goal: Maintain CPU at 50% │
│ │
│ CPU ▲ │
│ │ │
│ 80─┤ x x │
│ │ x x ← Too high → Scale OUT │
│ 50─┤──────────────────────────────────────── Target │
│ │ x │
│ 20─┤ x x ← Too low → Scale IN │
│ │ │
│ └─────────────────────────────────────────► Time │
│ │
│ AWS automatically adjusts the number of instances │
│ to maintain CPU around 50% │
│ │
│ Available metrics: │
│ • ASGAverageCPUUtilization │
│ • ASGAverageNetworkIn │
│ • ASGAverageNetworkOut │
│ • ALBRequestCountPerTarget │
│ │
└─────────────────────────────────────────────────────────────┘
# Create a Target Tracking policy
aws autoscaling put-scaling-policy \
--auto-scaling-group-name my-asg \
--policy-name cpu-target-policy \
--policy-type TargetTrackingScaling \
--target-tracking-configuration '{
"TargetValue": 50.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageCPUUtilization"
}
}'
4. Step Scaling
Scaling in steps according to load intensity.
┌─────────────────────────────────────────────────────────────┐
│ STEP SCALING │
├─────────────────────────────────────────────────────────────┤
│ │
│ Step scaling rules: │
│ │
│ CPU% Action │
│ ───── ────── │
│ 0-40% Remove 2 instances │
│ 40-60% No action │
│ 60-80% Add 1 instance │
│ 80-100% Add 3 instances │
│ │
│ ┌───────────────────────────────────────────────────┐ │
│ │ │ │
│ │ 100% ┬─────────────────────────────┐ │ │
│ │ │ +3 instances │ │ │
│ │ 80% ┼─────────────────────────────┼ │ │
│ │ │ +1 instance │ │ │
│ │ 60% ┼─────────────────────────────┼ │ │
│ │ │ (nothing) │ │ │
│ │ 40% ┼─────────────────────────────┼ │ │
│ │ │ -2 instances │ │ │
│ │ 0% └─────────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Comparison of Types
| Type | When to use | Reactivity |
|---|---|---|
| Manual | Known events in advance | Immediate |
| Scheduled | Predictable patterns (schedules) | Planned |
| Target Tracking | Variable load, recommended by default | ~3 min |
| Step | Proportional response to load | ~3 min |
Scaling Behavior
Scale Out vs Scale In
┌─────────────────────────────────────────────────────────────┐
│ SCALE OUT vs SCALE IN │
├─────────────────────────────────────────────────────────────┤
│ │
│ SCALE OUT (add) │
│ ─────────────────── │
│ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
│ │ 1 │ │ 2 │ → │ 1 │ │ 2 │ │ 3 │ │ 4 │ │
│ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ │
│ 2 instances 4 instances │
│ │
│ Behavior: │
│ • Fast (instances launched as soon as threshold reached) │
│ • No default cooldown for Target Tracking │
│ │
│ ──────────────────────────────────────────────────────── │
│ │
│ SCALE IN (remove) │
│ ────────────────── │
│ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
│ │ 1 │ │ 2 │ │ 3 │ │ 4 │ → │ 1 │ │ 2 │ │
│ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ │
│ 4 instances 2 instances │
│ │
│ Behavior: │
│ • Slower (avoids oscillations) │
│ • Default cooldown of 300s │
│ • Termination policy determines which instance │
│ │
└─────────────────────────────────────────────────────────────┘
Termination Policy
Which instance to terminate first during scale in?
| Policy | Behavior |
|---|---|
| Default | Oldest AZ with most instances, then oldest config |
| OldestInstance | Oldest instance |
| NewestInstance | Newest instance |
| OldestLaunchConfig | Oldest launch config |
| ClosestToNextInstanceHour | Closest to next billing hour |
Cooldown and Warmup
┌─────────────────────────────────────────────────────────────┐
│ COOLDOWN AND WARMUP │
├─────────────────────────────────────────────────────────────┤
│ │
│ COOLDOWN (after scaling) │
│ ──────────────────────── │
│ Period where no additional scaling occurs │
│ → Avoids oscillations │
│ │
│ Time ─────► │
│ │ │
│ │ Scale Cooldown Scale Cooldown │
│ │ OUT 300s IN 300s │
│ │ │ ├─────┤ │ ├─────┤ │
│ │ ▼ │ │ ▼ │ │ │
│ │ ┌───┐ │ │ ┌───┐ │ │ │
│ │ │+2 │ │ no │ │-1 │ │ no │ │
│ │ │ │ │ scale│ │ │ │ scale│ │
│ │ │ │ │ │ │ │ │ │ │
│ │ └───┘ │ │ └───┘ │ │ │
│ └──────────┴─────┴────────┴───────┴─────┴──────────► │
│ │
│ WARMUP (before metrics) │
│ ──────────────────────── │
│ Time for a new instance to be "warm" │
│ → Its metrics don't count before warmup │
│ │
│ Default Target Tracking: 300 seconds │
│ │
└─────────────────────────────────────────────────────────────┘
Summary
| Concept | Description | Typical Value |
|---|---|---|
| Launch Template | Config for new instances | AMI + type + SG |
| ASG | Managed instance group | min:2, max:10 |
| Target Tracking | Automatic scaling on metric | CPU 50% |
| Cooldown | Pause after scaling | 300s |
| Warmup | Delay before counting metrics | 300s |
Next section: Scaling Policies