Auto Scaling
Automatically add capacity under load and remove it when quiet to stay fast and cost-efficient.
Auto scaling adjusts how much compute you run based on demand. Under a traffic spike it adds tasks or instances; when things quiet down it removes them, so you pay only for what you need.
What can scale
- ECS service auto scaling — add/remove Fargate tasks on CPU or request count.
- EC2 Auto Scaling Groups — add/remove instances behind a load balancer.
- Lambda — scales concurrency automatically with no configuration.
Target tracking
The simplest policy is target tracking: pick a metric and a target (e.g. keep average CPU at 50%) and AWS adds or removes capacity to hold it there.
Example
# Keep ECS service CPU near 50% by scaling tasks 2..10
aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--resource-id service/myapp/web \
--scalable-dimension ecs:service:DesiredCount \
--policy-name cpu-target \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration \
'TargetValue=50.0,PredefinedMetricSpecification={PredefinedMetricType=ECSServiceAverageCPUUtilization}'When to use it
- An e-commerce site uses EC2 Auto Scaling to add instances when CPU exceeds 70% during Black Friday traffic and removes them when traffic subsides.
- An ECS service uses Application Auto Scaling based on ALB request count per target so container count tracks actual load precisely.
- A data pipeline uses scheduled scaling to pre-warm extra capacity before a nightly batch job and scale down after it completes.
More examples
Create EC2 Auto Scaling group
Creates an auto scaling group with a min of 2 and max of 10 instances using an existing launch template.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name myapp-asg \
--launch-template LaunchTemplateId=lt-0abc123,Version='$Latest' \
--min-size 2 \
--max-size 10 \
--desired-capacity 2 \
--vpc-zone-identifier 'subnet-aaa,subnet-bbb' \
--health-check-type ELB \
--health-check-grace-period 60CPU-based scaling policy
Adds a target-tracking policy that automatically adjusts instance count to keep average CPU around 70%.
aws autoscaling put-scaling-policy \
--auto-scaling-group-name myapp-asg \
--policy-name cpu-target-tracking \
--policy-type TargetTrackingScaling \
--target-tracking-configuration \
'PredefinedMetricSpecification={PredefinedMetricType=ASGAverageCPUUtilization},TargetValue=70.0'ECS Application Auto Scaling
Registers the ECS service as a scalable target and adds CPU-based target-tracking so task count scales with demand.
aws application-autoscaling register-scalable-target \
--service-namespace ecs \
--resource-id service/prod/myapp \
--scalable-dimension ecs:service:DesiredCount \
--min-capacity 2 \
--max-capacity 20
aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--resource-id service/prod/myapp \
--scalable-dimension ecs:service:DesiredCount \
--policy-name ecs-cpu-tracking \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration \
'PredefinedMetricSpecification={PredefinedMetricType=ECSServiceAverageCPUUtilization},TargetValue=60.0'
Discussion