Sub topic 8
Auto Scaling groups that span AZs
This chapter assumes you know EC2 launch templates, VPC subnets, and Application Load Balancers (ALBs). It builds on the multi-AZ subnet layout and cross-zone load balancing from earlier chapters.
What the group does for availability
An Auto Scaling group (ASG) keeps a number of EC2 instances running. For availability, it does two jobs. It spreads instances across the Availability Zones (AZs) you give it, and it replaces instances that fail health checks.
The AZs come from the subnets in VPCZoneIdentifier. Each subnet lives in one AZ, so listing a private subnet from two AZs gives you a two-AZ group. A group with subnets in only one AZ is a single point of failure, however many instances it runs.
A two-AZ group
Resources:
WebAsg:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
MinSize: "2"
MaxSize: "6"
DesiredCapacity: "2"
VPCZoneIdentifier:
- !Ref PrivateSubnetA
- !Ref PrivateSubnetB
LaunchTemplate:
LaunchTemplateId: !Ref WebLaunchTemplate
Version: !GetAtt WebLaunchTemplate.LatestVersionNumber
TargetGroupARNs:
- !Ref WebTargetGroup
HealthCheckType: ELB
HealthCheckGracePeriod: 120Points to note:
MinSize: 2with two subnets means one instance per AZ at minimum.TargetGroupARNsregisters each new instance with the ALB target group automatically.HealthCheckType: ELBmakes the group use the load balancer's health check as well as the EC2 status checks. Without it, an instance whose application has crashed still looks healthy to EC2 and is never replaced.HealthCheckGracePeriod: 120gives a new instance 120 seconds to boot before health checks can mark it unhealthy. Set it longer than your real startup time, or the group will replace instances that are still starting, in a loop.
How balancing and rebalancing work
The group launches new instances in the AZ with the fewest instances. If an AZ has no capacity or a launch fails there, the group tries another AZ.
When an AZ becomes unhealthy or you remove a subnet, the group launches replacements in the remaining AZs. Later, if the distribution is uneven, it rebalances by launching a new instance in the under-filled AZ before terminating one in the over-filled AZ. This keeps capacity from dipping, but the group can briefly exceed its desired count.
Common errors
- One AZ only. The subnet list covers a single AZ, often because a second subnet was never added to the template.
- Too little headroom. With
MinSize: 2, losing an AZ leaves one instance carrying all traffic until scaling reacts. Chapter on static stability covers sizing for this. - Default health checks. EC2-only checks do not detect a failed application.
- Short grace period. Instances are terminated before they finish booting.
- Capacity limits in one AZ. A single instance type may be unavailable in one AZ. Allowing several instance types in a mixed instances policy reduces this risk.
Exercise
An ASG has MinSize 2, subnets in two AZs, and HealthCheckType EC2. The application process on one instance crashes, but the instance itself keeps running. What happens, and what change fixes it?
Show answer
Nothing happens. EC2 status checks still pass, so the group considers the instance healthy and keeps it, while the load balancer stops sending it traffic and capacity is reduced. Set HealthCheckType to ELB so the load balancer's failed health check causes the group to terminate and replace the instance.