Sub topic 12
Auto Scaling groups and capacity limits
This chapter assumes you can launch EC2 instances from a launch template and that you know what a VPC subnet is. It covers how an Auto Scaling group (ASG) decides how many instances to run, what bounds that number, and which errors you will meet first. Target groups and scaling policies get their own chapters.
What an Auto Scaling group does
An ASG is a managed fleet of EC2 instances. You give it three things:
- A launch template, which says what to launch.
- One or more subnets, which say where to launch it.
- Three capacity numbers, which say how many.
The group then runs a control loop. It compares the number of healthy in-service instances with the desired capacity. It launches instances when there are too few and terminates instances when there are too many. It also replaces any instance that fails a health check.
Minimum, maximum and desired capacity
| Setting | Meaning | Who changes it |
|---|---|---|
| Minimum | The floor. The group never terminates below this number. | You |
| Maximum | The ceiling. The group never launches above this number. | You |
| Desired | The number the group is trying to reach right now. | You, a scaling policy, or a scheduled action |
The rule is min <= desired <= max. Scaling policies move the desired value inside that range and never outside it. The maximum is your safety limit against runaway cost, because a misconfigured policy cannot go past it.
If you edit the bounds so that the current desired value falls outside them, the group adjusts desired to fit. Raising the minimum to 4 while desired is 2 makes desired 4, and the group launches two instances. A direct request for a desired value outside the bounds is rejected with a ValidationError.
When min equals max, the group is a fixed-size fleet. This is still useful, because the group replaces failed instances automatically.
Creating a group
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"If you omit --desired-capacity, it defaults to the minimum. The --vpc-zone-identifier list decides which Availability Zones the group uses. Choose subnets in at least two zones, and pick subnets that are all private or all public to match your design.
Using Version='$Latest' means a later launch template edit changes what new instances look like. For production groups, pin a specific version number so that changes are deliberate.
Spreading across Availability Zones
The ASG tries to keep the number of instances per zone even. A new instance goes to the zone with the fewest. If a zone becomes unavailable, the group launches replacements in the remaining zones. When the zone recovers, it rebalances by launching in the recovered zone and then terminating instances elsewhere.
Rebalancing launches before it terminates. For a short time you can run above desired capacity, and that overshoot is still bounded by a margin above the maximum. Make sure your quotas leave room for it.
Instance states
Only InService instances count toward serving capacity. An instance in Standby stays in the group but receives no traffic and is not health-checked. This makes it a safe state for debugging or patching. Standby instances still count toward the group's capacity numbers unless you decrement desired when you enter standby.
Changing capacity by hand
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name web-asg \
--min-size 3 \
--max-size 10
aws autoscaling set-desired-capacity \
--auto-scaling-group-name web-asg \
--desired-capacity 5Manual changes last only until the next scaling event. If a target tracking policy is attached, it will move desired back to whatever its metric demands. To hold a manual value, change min and max as well, or suspend the scaling processes.
Inspecting the group
aws autoscaling describe-auto-scaling-groups \
--auto-scaling-group-names web-asg \
--query "AutoScalingGroups[0].{Min:MinSize,Max:MaxSize,Desired:DesiredCapacity,Instances:Instances[].[InstanceId,AvailabilityZone,LifecycleState,HealthStatus]}"
aws autoscaling describe-scaling-activities \
--auto-scaling-group-name web-asg \
--max-items 5describe-scaling-activities is the most useful command in this chapter. Every launch and termination appears there with a status and a cause, and failed launches include the underlying error message.
Health checks and replacement
The group runs its own health check on every instance. The default type is EC2, which marks an instance unhealthy only if the instance is stopped, terminated or failing its EC2 status checks. An application that has crashed on a running instance still counts as healthy under this type. The ELB type adds load balancer health results, and the integration chapter covers it.
The grace period (default 300 seconds) is the time after an instance enters service before the group acts on its health. Set it longer than your slowest boot and bootstrap, or the group will terminate instances that are still starting up and then launch more of them in the same loop.
Protecting and limiting instances
- Scale-in protection prevents a specific instance from being chosen when the group scales in. It does not stop health replacement, rebalancing is unaffected by it only partly, and a manual termination still works.
- Maximum instance lifetime replaces instances older than a set number of seconds. The minimum allowed value is 86,400 (one day). This is useful for forcing fresh AMIs and clearing memory leaks.
- Suspended processes let you pause one behavior, such as
Launch,Terminate,HealthCheck,ReplaceUnhealthyorAZRebalance, without deleting the group.
aws autoscaling suspend-processes \
--auto-scaling-group-name web-asg \
--scaling-processes AZRebalance
aws autoscaling resume-processes \
--auto-scaling-group-name web-asg \
--scaling-processes AZRebalanceCommon errors
- The group never reaches desired capacity. Check
describe-scaling-activities. The usual causes areVcpuLimitExceeded(your EC2 vCPU service quota is too low),InsufficientInstanceCapacity(the zone has no capacity for that instance type), a deleted AMI, security group or key pair named in the template, or an instance profile the group cannot pass. - Launch loops. Instances start, fail health checks, are terminated and are replaced, over and over. The cause is nearly always a grace period shorter than the boot time, or a bootstrap script that fails.
- Uneven zones. If one subnet runs out of free IP addresses, the group launches only in the other zones. Give each subnet enough address space.
- Desired capacity keeps changing. A scaling policy or scheduled action is overriding your manual value.
Account-level limits also apply. There is a quota on the number of groups per Region, and each EC2 instance family draws on a vCPU quota. Request increases before you need them.
Exercise
An ASG has min 2, max 6 and desired 4. You run update-auto-scaling-group with --min-size 5 and make no other change. What happens to desired capacity, and what do you do if you want the group to stay at 4 instances?
Show answer
Desired capacity is raised to 5, because it must stay between min and max, and the group launches one instance. To keep 4 instances, leave the minimum at 4 or lower, or set min and max to 4 for a fixed-size group.
Exercise
A new group stays at 0 in-service instances. describe-scaling-activities shows repeated launch failures with the message VcpuLimitExceeded. Name the cause and one fix.
Show answer
The account's EC2 vCPU service quota for that instance family is exhausted. Request a quota increase, or change the launch template to a different instance type or family that has quota available.