Auto Scaling groups
How an Auto Scaling group keeps the right number of EC2 instances running, with min/desired/max capacity, launch templates, health checks and a load balancer.
What it is
Real traffic goes up and down: busy in the day, quiet at night, huge on sale days. In the cloud, servers can be created and removed in minutes, so there's no need to pay for peak capacity all the time.
An Auto Scaling group (ASG) manages a fleet of EC2 instances. It can:
- scale out (add instances) when the load increases;
- scale in (remove instances) when the load decreases;
- keep the number of instances between a minimum and a maximum;
- register new instances with a load balancer automatically;
- replace an instance that fails or is terminated.
Auto Scaling groups are free. Billing is only for the EC2 instances (and other resources) they run.
Capacity: minimum, desired, maximum
- Minimum: the group never goes below this, even with no load. Here, 2.
- Desired: how many it runs right now. Scaling policies (or an operator) change this number. Here, 4.
- Maximum: the ceiling, which also caps the bill. Here, 7.
If an instance dies, the group launches a new one to get back to the desired number.
With a load balancer, across AZs
- The group spreads instances evenly across the AZs (subnets) it's given, and rebalances if one AZ ends up with more.
- It registers every new instance with the load balancer's target group, and deregisters instances it removes.
- The load balancer sends traffic only to healthy instances.
- If ELB health checks are turned on for the group, an instance the load balancer marks unhealthy is terminated and replaced.
Health checks
- EC2 status checks are always used: a stopped or impaired instance is replaced.
- ELB health checks (optional, recommended behind a load balancer) also replace instances whose application fails the target group's health check.
- A health check grace period gives new instances time to boot before they're judged.
What a group needs
- A launch template says how to launch each instance: AMI and instance type, user data, EBS volumes, security groups, SSH key pair, IAM role (instance profile), network and subnet settings. Launch templates have versions, so they can be changed safely.
- The group's own settings say how many and where: min, desired and max size, subnets (and so AZs), load balancer target groups, health checks, and scaling policies.
- A group can also mix instance types and On-Demand with Spot (a mixed instances policy), which is a cheap and resilient way to run fault-tolerant work.
Updated since the slides
The older launch configurations are on their way out. They haven't supported instance types released since 1 January 2023, and accounts created on or after 1 October 2024 can't create them at all. Use launch templates.
Exam tips
- "Automatically replace failed instances", "maintain a fixed number of instances" → Auto Scaling group (even with min = max).
- "Replace instances the load balancer marks unhealthy" → turn on ELB health checks for the group.
- "Highly available across AZs" → the group's subnets span at least two AZs.
- "Change the AMI or instance type for new instances" → a new launch template version (then an instance refresh to replace existing ones).
- "Cheapest for fault-tolerant work, keep a baseline" → mixed instances policy with On-Demand base capacity plus Spot.
Common traps
- "Auto Scaling groups cost extra." They're free.
- "Edit the launch configuration to change instances." Launch configurations can't be edited; and launch templates are the recommended choice anyway.
- "The group replaces instances that fail the ALB health check by default." Only EC2 status checks by default; ELB health checks must be turned on.
Hands-on lab: a self-healing group behind an ALB
Goal: create a launch template and an Auto Scaling group of two instances behind an ALB, then terminate one and watch it come back.
Costs money
Two t3.micro instances, an ALB and its public IPs: under $0.10 an hour. At the end, delete the group (which terminates its instances) and the load balancer.
Create a launch template
EC2 → Launch templates → Create launch template: name saa-lt, tick Auto Scaling guidance, Amazon Linux 2023, t3.micro, key pair saa-lab-key, security group saa-web-sg, and paste the web server script from the EC2 basics lab into Advanced details → User data. Create.
Create the group with a load balancer
Auto Scaling groups → Create: name saa-asg, template saa-lt. Choose the default VPC and two or more subnets. Choose Attach to a new load balancer → Application Load Balancer, internet-facing, listener HTTP 80, create a target group saa-asg-tg. Turn on Elastic Load Balancing health checks. Group size: desired 2, min 2, max 4. No scaling policy yet. Tag Project=saa-lab and create.
Test it
When both instances are InService and healthy in the target group, open the new ALB's DNS name and refresh: the two hostnames alternate. (If the page doesn't load, make sure saa-web-sg allows HTTP from the ALB's security group or from anywhere.)
Break it
In Instances, terminate one of the group's instances. Open the group's Activity tab: within a minute or two it launches a replacement, which registers with the target group and starts serving.
Expected result: the group always returns to its desired capacity of 2, by itself.
Clean up: keep the group for the scaling policies lab, or delete the Auto Scaling group (this terminates its instances), then the load balancer, the target group and the launch template.
AWS documentation
SSL/TLS, SNI and deregistration delay
HTTPS on load balancers with ACM certificates, serving many domains with SNI, security policies, and connection draining.
Scaling policies
How Auto Scaling groups decide to scale, with CloudWatch alarms, target tracking, step, scheduled and predictive policies, good metrics, cooldowns, warm pools, instance refresh, and AWS Auto Scaling.