Sub topic 11
Auto Scaling groups and capacity limits
This chapter assumes you know EC2 instances, AMIs, security groups, and VPC subnets in different Availability Zones. It also assumes you can run AWS CLI commands. You will see how an Auto Scaling group (ASG) keeps a fleet of instances at the size you set.
What an Auto Scaling group does
An ASG is a managed set of EC2 instances built from one launch template. It compares the number of running instances with the number you want, and it launches or terminates instances to close the gap. It also replaces instances that fail a health check. The launch template supplies the AMI, instance type, and security groups. The ASG supplies the "how many" and "where".
The three capacity values
Three numbers control the size of the group.
| Setting | Meaning |
|---|---|
| Minimum | The fewest instances the group will ever run. |
| Maximum | The most instances the group will ever run. |
| Desired capacity | The number the group tries to run right now. |
Desired capacity must always be between the minimum and the maximum. Scaling policies change the desired capacity. They cannot move it outside the limits. The minimum protects availability, because the group never scales in below it. The maximum protects your budget, because a runaway metric cannot launch unlimited instances.
Creating a group
This command creates a group of 2 to 6 instances across two subnets. It registers them with a target group and uses load balancer health checks.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name web-asg \
--launch-template LaunchTemplateName=web-lt,Version='$Latest' \
--min-size 2 \
--max-size 6 \
--desired-capacity 2 \
--vpc-zone-identifier "subnet-aaaa1111,subnet-bbbb2222" \
--target-group-arns "$TG_ARN" \
--health-check-type ELB \
--health-check-grace-period 300Several parts of this command matter:
--vpc-zone-identifierlists the subnets. The ASG spreads instances across their Availability Zones and rebalances if one zone becomes unavailable. Use subnets in at least two zones.Version='$Latest'makes new launches use the newest template version. You can pin a number instead when you want predictable changes.--health-check-type ELBmakes the ASG treat an instance as unhealthy when the load balancer says so. The default type, EC2, only notices a stopped or failed instance, not a broken application.--health-check-grace-period 300gives a new instance 300 seconds to boot before health checks can cause its replacement.
Changing capacity by hand
You can set desired capacity directly to test the limits.
aws autoscaling set-desired-capacity \
--auto-scaling-group-name web-asg \
--desired-capacity 4A value of 8 fails here, because the maximum is 6. To raise the ceiling, call update-auto-scaling-group --max-size 10.
Common errors
- Desired capacity outside the limits. The API rejects it. Change the limits first.
- Minimum equal to maximum. The group heals instances but never scales. This is valid for a fixed-size fleet, but it is a surprise if you expected scaling.
- Grace period too short. Slow-booting instances are terminated before they finish starting, and the group launches replacements in a loop.
- Only one subnet. The group has no protection against the loss of a zone.
Exercise
An ASG has min 2, max 6, and desired 3. A scaling policy asks for 9 instances. How many instances does the group run, and why?
Show answer
It runs 6. Desired capacity is clamped to the maximum, so the group never launches more than 6 instances.