Skip to Content

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

An Auto Scaling group with a minimum of 2 instances, a desired capacity of 4 running now, and room to scale out to a maximum of 7 An Auto Scaling group with a minimum of 2 instances, a desired capacity of 4 running now, and room to scale out to a maximum of 7

The three numbers that size a group.
  • 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

Users reach an ALB that health-checks instances in two AZs; the Auto Scaling group adds new instances to the target group and replaces failed ones Users reach an ALB that health-checks instances in two AZs; the Auto Scaling group adds new instances to the target group and replaces failed ones

The standard highly available web tier.
  1. The group spreads instances evenly across the AZs (subnets) it's given, and rebalances if one AZ ends up with more.
  2. It registers every new instance with the load balancer's target group, and deregisters instances it removes.
  3. The load balancer sends traffic only to healthy instances.
  4. 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 holds the AMI, instance type, user data, EBS volumes, security groups, key pair, IAM role and network settings; the group adds min, desired and max, subnets and AZs, target group, health checks and scaling policies A launch template holds the AMI, instance type, user data, EBS volumes, security groups, key pair, IAM role and network settings; the group adds min, desired and max, subnets and AZs, target group, health checks and scaling policies

How to launch an instance, and how many to run.
  • 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

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt