Sub topic 14
Scaling a service with target tracking
This chapter assumes you have a running ECS service on Fargate, as built in the earlier chapters, and that you can use the AWS CLI. You will attach target tracking auto scaling to that service so the number of tasks follows load.
How it works
ECS services keep a desired count of tasks running. Service auto scaling changes that desired count for you. It uses Application Auto Scaling, a separate AWS service that scales several resource types.
Target tracking works like a thermostat. You pick a metric and a target value, for example average CPU at 60 percent. Application Auto Scaling creates and manages CloudWatch alarms for you. When the metric rises above the target, it adds tasks. When the metric stays below the target, it removes tasks.
Step 1: Register the scalable target
The scalable target sets the limits. Auto scaling never goes below the minimum or above the maximum.
aws application-autoscaling register-scalable-target \
--service-namespace ecs \
--scalable-dimension ecs:service:DesiredCount \
--resource-id service/demo/web \
--min-capacity 2 \
--max-capacity 10The resource ID has the form service/<cluster>/<service>. A minimum of 2 keeps tasks in two Availability Zones.
Step 2: Define the policy
Put the target in a JSON file named scaling-policy.json.
{
"TargetValue": 60.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ECSServiceAverageCPUUtilization"
},
"ScaleOutCooldown": 60,
"ScaleInCooldown": 120
}Then attach it.
aws application-autoscaling put-scaling-policy \
--policy-name cpu-target-60 \
--service-namespace ecs \
--scalable-dimension ecs:service:DesiredCount \
--resource-id service/demo/web \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration file://scaling-policy.jsonThe cooldowns make scaling asymmetric. A 60-second scale-out cooldown lets the service add capacity quickly. A 120-second scale-in cooldown removes tasks more cautiously, so a brief dip does not shrink the service just before the next spike.
Choosing a metric
| Metric | Best for |
|---|---|
| `ECSServiceAverageCPUUtilization` | CPU-bound services |
| `ECSServiceAverageMemoryUtilization` | Memory-bound services |
| `ALBRequestCountPerTarget` | Web services where traffic drives load |
ALBRequestCountPerTarget also needs a ResourceLabel that identifies the load balancer and target group. Use a custom metric when none of these match your bottleneck, such as queue depth for a worker.
Common errors
- Scaling lags behind load. Target tracking waits for several consecutive one-minute datapoints before scaling out, and many more before scaling in. It reacts in minutes, not seconds, so keep headroom in the target.
- Tasks never scale in. Scale-in is deliberately slow. Check that the metric is really below the target and that the count is above the minimum.
- The count resets after a deploy. If your infrastructure template hardcodes
desired-count, a redeploy can override what auto scaling chose. Leave the desired count out of the template after the first creation, or let the scalable target own it. - New tasks are killed while starting. Slow startup combined with load balancer health checks can cause this. Keep a health check grace period long enough, as in the earlier chapter.
- Scaling stops at a limit. A flat line at 10 tasks means you hit the maximum capacity. Raise it if that is not intended.
Exercise
Your service runs at 90 percent CPU with a target of 60 percent, but the task count stays at 10. The scalable target has a minimum of 2 and a maximum of 10. What is the cause, and what do you change?
Show answer
The service has reached the maximum capacity of 10, so auto scaling cannot add tasks. Raise --max-capacity with register-scalable-target, or reduce the load per task. Check the account's Fargate task quota too.