Sub topic 15
Scaling policies: target tracking, step, scheduled and predictive
This chapter assumes you can create an Auto Scaling group (ASG) attached to a target group, and that you know what a CloudWatch metric and alarm are. A scaling policy tells the ASG when and how far to change its desired capacity. The ASG always stays inside its minimum and maximum size, whichever policy fires.
The four policy types
| Policy | Trigger | You manage alarms? | Best for |
|---|---|---|---|
| Target tracking | A metric drifting from a target value | No, the ASG creates them | Most steady, load-driven workloads |
| Step scaling | A CloudWatch alarm you define | Yes | Different responses for different breach sizes |
| Scheduled | A date and time | No | Known, predictable events |
| Predictive | A forecast built from history | No | Recurring daily or weekly cycles |
Target tracking
Target tracking works like a thermostat. You pick a metric and a target value, and the ASG adds or removes instances to keep the metric near that value. It creates and manages the CloudWatch alarms itself. Do not edit or delete them.
Predefined metrics include ASGAverageCPUUtilization, ASGAverageNetworkIn, ASGAverageNetworkOut and ALBRequestCountPerTarget. The request-count metric is usually the better choice for web tiers, because it measures load directly. CPU only reflects load indirectly.
The configuration from the earlier chapter keeps requests per target near 1000:
{
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ALBRequestCountPerTarget",
"ResourceLabel": "app/web-alb/50dc6c495c0c9188/targetgroup/web-tg/943f017f100becff"
},
"TargetValue": 1000.0
}aws autoscaling put-scaling-policy \
--auto-scaling-group-name web-asg \
--policy-name requests-per-target-1000 \
--policy-type TargetTrackingScaling \
--target-tracking-configuration file://config.jsonBehaviors to know:
- Scale-out is aggressive and scale-in is cautious. The ASG reacts to a few breaching datapoints when scaling out, but it waits for a much longer run of low datapoints before scaling in. This avoids removing capacity during a brief lull.
- The metric must change in proportion to capacity. Total request count does not qualify, because adding instances does not lower it. Per-target and average metrics do.
- With several target tracking policies, the ASG scales out if any one of them asks for more capacity. It scales in only when all of them agree.
- Scale-in can be disabled for a policy with
DisableScaleIn. This is useful if you want a separate mechanism, such as a schedule, to shrink the group.
Step scaling
Step scaling gives you finer control. You create the CloudWatch alarm and the policy, and you link them. The policy holds step adjustments, which are ranges measured as offsets from the alarm threshold.
The next commands create a scale-out policy. If the alarm threshold is 60 percent CPU, a breach of up to 20 points adds 1 instance and a larger breach adds 3.
POLICY_ARN=$(aws autoscaling put-scaling-policy \
--auto-scaling-group-name web-asg \
--policy-name cpu-step-out \
--policy-type StepScaling \
--adjustment-type ChangeInCapacity \
--metric-aggregation-type Average \
--estimated-instance-warmup 180 \
--step-adjustments \
MetricIntervalLowerBound=0,MetricIntervalUpperBound=20,ScalingAdjustment=1 \
MetricIntervalLowerBound=20,ScalingAdjustment=3 \
--query PolicyARN --output text)
aws cloudwatch put-metric-alarm \
--alarm-name web-asg-cpu-high \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=AutoScalingGroupName,Value=web-asg \
--statistic Average \
--period 60 \
--evaluation-periods 3 \
--threshold 60 \
--comparison-operator GreaterThanThreshold \
--alarm-actions "$POLICY_ARN"The first step starts at lower bound 0, meaning 60 itself. A bound is relative to the threshold, not an absolute CPU value. A scale-in policy is a separate policy with negative adjustments, attached to a separate alarm with a lower threshold.
Three points differ from target tracking:
- Adjustment types are
ChangeInCapacity,PercentChangeInCapacityandExactCapacity. - Step ranges must not overlap, and there must be no gaps.
- AWS recommends target tracking for most cases. Choose step scaling when you need an asymmetric or non-proportional response, or a metric that target tracking cannot use.
Never run target tracking and step scaling on the same metric for the same group. They will fight each other.
Scheduled scaling
A scheduled action changes the minimum, maximum or desired capacity at a set time. Use it when you know demand in advance, such as a sale or a business-hours pattern.
aws autoscaling put-scheduled-update-group-action \
--auto-scaling-group-name web-asg \
--scheduled-action-name weekday-morning \
--recurrence "0 8 * * 1-5" \
--time-zone "Europe/London" \
--min-size 4 \
--desired-capacity 4The recurrence is a Unix cron expression. Without --time-zone, it is evaluated in UTC. Raising the minimum is often better than raising only the desired capacity. Dynamic policies can scale in below a raised desired value, but they can never go below the minimum. Scheduled actions and dynamic policies work together. The schedule sets the floor, and target tracking handles load above it.
Predictive scaling
Predictive scaling analyzes up to 14 days of CloudWatch history for a recurring pattern. It needs at least 24 hours of data. It forecasts load 48 hours ahead and launches instances before the demand arrives. This helps when instances take a long time to boot or warm up.
Start in forecast-only mode and compare the forecast with real load before you let it act:
{
"MetricSpecifications": [
{
"TargetValue": 50,
"PredefinedMetricPairSpecification": {
"PredefinedMetricType": "ASGCPUUtilization"
}
}
],
"Mode": "ForecastOnly"
}aws autoscaling put-scaling-policy \
--auto-scaling-group-name web-asg \
--policy-name predictive-cpu \
--policy-type PredictiveScaling \
--predictive-scaling-configuration file://predictive.jsonChange Mode to ForecastAndScale once the forecasts look right. Predictive scaling only adds capacity. It never scales in, so pair it with a target tracking policy that handles scale-in and unexpected spikes.
Common mistakes
- Using total request count as a target tracking metric.
- Forgetting
--estimated-instance-warmupon step policies, so new instances are counted before they carry traffic. - Writing step bounds as absolute metric values instead of offsets from the alarm threshold.
- Setting a scheduled time without a time zone and getting UTC.
- Enabling predictive scaling on a group with less than a day of history or an irregular load pattern.
Exercise
Your ASG has a target tracking policy on ALBRequestCountPerTarget with a target of 1000, plus a scheduled action that sets the minimum size to 6 at 08:00 on weekdays. At 09:00 the traffic is low and would need only 3 instances. How many instances run, and why?
Show answer
At least 6 instances run. Target tracking can scale in to 3, but the ASG never goes below the minimum size, and the scheduled action raised the minimum to 6. The schedule sets the floor and target tracking only adjusts capacity above it.
Exercise
A step scaling policy is attached to an alarm with a threshold of 70 percent CPU. The steps are lower bound 0 to upper bound 10 with adjustment +1, and lower bound 10 with adjustment +4. CPU reaches 85 percent. How many instances are added?
Show answer
Four. The breach is 85 minus 70, which is 15 points above the threshold. That falls in the step with lower bound 10, so the adjustment is +4, subject to the maximum size.