Auto Scaling turns individual servers into a fleet that replaces failures and follows demand without manual work.
An Auto Scaling group (ASG) keeps a number of instances running between min and max, aiming at desired. It launches them from a launch template (AMI, instance type, security groups, IAM profile, user data; templates are versioned) into subnets you list, balancing across AZs.
ASGs heal: if an instance fails its health check it is terminated and replaced. Set the health check type to ELB when you attach target groups, so an instance whose app is broken (but whose VM is fine) is replaced too, and give it a grace period to boot.
Scaling policies change desired:
- Target tracking: keep a metric near a target, such as average CPU at 50% or
ALBRequestCountPerTargetat 1,000. The default choice. - Step scaling: add or remove capacity in steps based on CloudWatch alarm thresholds.
- Scheduled actions: known patterns, such as scaling up before business hours.
- Predictive scaling: forecasts from history and scales ahead of the curve.
Rolling out a new AMI is an instance refresh: the ASG replaces instances in batches while keeping a minimum healthy percentage. Mixed instances policies combine several instance types and On-Demand with Spot capacity for big savings. Lifecycle hooks pause instances while launching or terminating, for example to drain work or register with other systems; warm pools keep pre-initialised instances for fast scale-out.
The ASG only works if instances are disposable: no state on local disks, configuration fully automated, and logs shipped off the box.
Example
aws ec2 create-launch-template --launch-template-name web --launch-template-data '{
"ImageId": "ami-0a1b2c3d4e5f67890", "InstanceType": "m7g.large",
"SecurityGroupIds": ["sg-0app"], "IamInstanceProfile": {"Name": "web-app"},
"MetadataOptions": {"HttpTokens": "required"}}'
aws autoscaling create-auto-scaling-group --auto-scaling-group-name web \
--launch-template 'LaunchTemplateName=web,Version=$Latest' \
--min-size 2 --max-size 10 --desired-capacity 2 \
--vpc-zone-identifier "subnet-priv-a,subnet-priv-b" \
--target-group-arns "$TG" --health-check-type ELB --health-check-grace-period 120
aws autoscaling put-scaling-policy --auto-scaling-group-name web \
--policy-name cpu-50 --policy-type TargetTrackingScaling \
--target-tracking-configuration '{"TargetValue": 50,
"PredefinedMetricSpecification": {"PredefinedMetricType": "ASGAverageCPUUtilization"}}'
# Roll out a new launch template version
aws autoscaling start-instance-refresh --auto-scaling-group-name web \
--preferences '{"MinHealthyPercentage": 90}'Remember
- ASG = launch template + subnets in several AZs + min/desired/max
- Use ELB health checks so broken apps get replaced
- Target tracking is the default scaling policy
- Instance refresh rolls out new AMIs safely
Watch out for
- Using EC2 health checks only, so instances with a crashed app keep running and failing requests.