Building Self-Healing Fleets with Auto Scaling Groups
This lesson explains how Auto Scaling groups give EC2 fleets elasticity and the ability to heal themselves.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how an Auto Scaling group maintains capacity.
- Describe launch templates, mixed instances policies and instance refresh.
- Explain how scale-in chooses instances.
- Explain how to size a group to survive the loss of an Availability Zone.
How a group works
An Auto Scaling group keeps a fleet of instances at the required size and in a healthy state. It maintains a desired number of instances between a minimum and a maximum, spread across the subnets, and therefore Availability Zones, that it is given, and it rebalances instances across zones when they become uneven.
By default, the group uses EC2 health checks, which detect failures of the instance itself; adding ELB health checks makes it also replace instances that fail the load balancer's application checks. The group registers new instances with its load balancer target groups automatically and deregisters them before termination.
Launch templates and fleets
Launch templates define the instances a group launches: the AMI, instance type, key pair, security groups, user data and instance profile. They are versioned, so a group can move to a new version deliberately; the older launch configurations are legacy.
A mixed instances policy lets one group combine several instance types and a chosen split between On-Demand and Spot capacity, which reduces cost and improves the chance of obtaining capacity. When the AMI or template changes, an instance refresh replaces the group's instances in rolling batches, keeping a minimum percentage healthy throughout.
Group features
Scale-in choices
When a group scales in, its default termination policy chooses which instance to remove. It first balances instances across Availability Zones, then prefers instances launched from the oldest launch template or configuration, and then chooses the instance closest to the next billing hour.
Custom termination policies, implemented with a Lambda function, can choose differently, for example removing the instance with the least work in progress. Scale-in protection marks specific instances that must not be terminated, such as those processing long jobs.
Surviving an AZ loss
The group relaunches in remaining zones; size so they can carry full load. To keep four instances after losing one zone, run six across three zones.
The following diagram shows the six-instance design after one zone fails.

Sizing for an AZ loss
Surviving the loss of an Availability Zone
An application needs at least 6 instances to serve peak traffic, in an Auto Scaling group across AZs.
| Design | Capacity after one AZ fails |
|---|---|
| 6 instances in 2 AZs (3 + 3) | 3: only half of what is needed until replacements launch |
| 9 instances in 3 AZs (3 + 3 + 3) | 6: enough, with no waiting |
| 6 instances in 3 AZs, relying on Auto Scaling to replace | 4 at first; recovery depends on launching new instances |
If the requirement is to keep full capacity through an AZ failure without waiting, the group must be over-provisioned across three AZs. If a short period of reduced capacity is acceptable, the cheaper design relies on Auto Scaling to replace lost instances in the remaining AZs.
Why should an Auto Scaling group use the load balancer's health checks?
Show answer
So that instances whose application fails the load balancer health check are replaced, not only instances that fail EC2 status checks.
Exam Tips
Remember for the exam
- For self-healing even of a single instance, use an ASG with minimum, maximum and desired capacity all set to 1.
- To replace instances that fail application health checks, enable ELB health checks on the ASG.
- To roll out a new AMI gradually, use instance refresh.
- To survive the loss of an Availability Zone, provision enough capacity in the remaining zones.
- To stop scale-in terminating an instance doing important work, use scale-in protection.