AWS Builder Center

EC2 Auto Scaling Explained for Beginners

Learn how EC2 Auto Scaling adds and removes servers automatically. Build a launch template, load balancer, and CPU scaling policy step by step

Software Engineering Undergraduate | AWS | Cloud & DevOps | Full-Stack Developer

The problem it solves

Picture an assignment submission portal that sits at three visitors all week. At 11:40 PM on deadline night, 400 students open it at once. One small server can't take that, so it slows to a crawl and then stops answering.
You have two bad options. Run a big server all month for one bad night, or run a small one and accept the crash. Auto Scaling is the third option. It watches your load, starts extra servers when things get busy, and shuts them down again when the rush is over. You pay for the big fleet only during the hours it exists.



Scaling up vs scaling out

There are two ways to handle more load, and the names get mixed up constantly:
Term
What it means
Example
Vertical scaling (scale up)
Replace the server with a bigger one
 t3.micro  to  t3.xlarge 
Horizontal scaling (scale out)
Add more servers of the same size
1 server becomes 4
Auto Scaling is horizontal. Vertical scaling needs a restart and has a ceiling, because there's only so big an instance can get. Horizontal scaling has no practical ceiling and, if one server dies, the others keep going.
It comes with one condition. Your app has to work when several copies run side by side. A site that stores uploaded files or login sessions on the server's own disk will behave strangely, since each copy has its own disk. Static sites and apps that keep their state in a database are fine.



The three pieces

An Auto Scaling setup has three parts, and you build them in this order:
Piece
What it does
Launch template
The recipe for a server: AMI, instance type, key pair, security group, startup script
Auto Scaling group (ASG)
The manager. It keeps the right number of servers running from that recipe
Scaling policy
The rule that tells the group when to add or remove servers
A load balancer sits in front of the group so visitors have a single address to hit. Without one, you'd have five servers with five different IPs and no way to share traffic between them.



Min, desired, and max

Every ASG has three numbers:
  • Minimum: the group never drops below this. If an instance dies, the group replaces it to get back to this number.
  • Desired: how many should be running right now. Scaling policies change this number up and down.
  • Maximum: the group never goes above this, no matter how busy things get.
Treat the maximum as a safety rail for your wallet. If a bug or an attack sends your CPU through the roof, the group will keep adding servers until it hits that cap. A max of 3 on a practice project means the worst case is three small instances, not thirty.



What it costs

Auto Scaling itself is free. You pay for what it launches and for the pieces around it:
Item
Cost
EC2 instances
The normal hourly price for each one, only while it exists
Application Load Balancer
An hourly charge plus a usage charge, roughly $16 a month in  us-east-1  before any traffic
CloudWatch alarms
Target tracking creates two for you. Standard alarms cost about $0.10 each per month, and some are covered by the free tier
The load balancer is the line that surprises people. It bills by the hour whether or not anyone visits, so it's the first thing to delete when you finish experimenting. Check the current pricing page, since numbers change.



Before you start

You need:
  1. An AWS account and a region you're comfortable using. This guide uses the default VPC, which already has subnets in several Availability Zones.
  1. A key pair, so you can log in to a test instance. The key pairs article covers it.
  1. A basic idea of security groups. We'll make two.
If those terms are new, read the security groups article first. The load balancer step depends on it.



Step 1: Create the security groups

Two groups, because the instances should only accept traffic that came through the load balancer.
Load balancer group. Go to EC2 → Security Groups → Create security group.
  • Name:  web-alb-sg 
  • Inbound rule: HTTP, port 80, source Anywhere-IPv4
Instance group. Create another one.
  • Name:  web-instance-sg 
  • Inbound rule 1: HTTP, port 80, source web-alb-sg (pick the security group from the dropdown instead of typing an IP)
  • Inbound rule 2: SSH, port 22, source My IP (only needed for the load test later)
That first rule is the useful one. It says "only the load balancer may talk to these servers on port 80." Visitors who find an instance's public IP get nothing.



Step 2: Create a launch template

  1. Open EC2 → Launch Templates → Create launch template
  1. Name it  web-template 
  1. AMI: Amazon Linux 2023
  1. Instance type:  t3.micro 
  1. Key pair: choose the one you already have
  1. Security groups: select  web-instance-sg 
  1. Scroll to Advanced details → User data and paste this:
#!/bin/bash
dnf install -y nginx
systemctl enable --now nginx

TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 60")
ID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/instance-id)

echo "<h1>Hello from $ID</h1>" > /usr/share/nginx/html/index.html
  1. Click Create launch template
User data is a script that runs once when an instance first boots. It's how every new server sets itself up without you touching it. This one installs Nginx and writes a page that shows the instance's own ID, which makes it obvious later when traffic starts rotating between servers.
Notice that you never launched an instance. The template is only a recipe. Nothing runs until the Auto Scaling group uses it.



Step 3: Create the Auto Scaling group

  1. Open EC2 → Auto Scaling Groups → Create Auto Scaling group
  1. Name it  web-asg  and select  web-template , then Next
  1. Network: keep the default VPC and select at least two subnets in different Availability Zones. This is what protects you if one data center has a bad day.
  1. Next, then under Load balancing choose Attach to a new load balancer
  1. Pick Application Load Balancer, Internet-facing, and name it  web-alb 
  1. Under Listeners and routing, create a new target group and name it  web-tg 
  1. Set the load balancer's security group to  web-alb-sg  (you may need to edit this on the load balancer after creation if the wizard doesn't offer it)
  1. Under Health checks, tick Turn on Elastic Load Balancing health checks
  1. Next, then set the group size:
Setting
Value
Desired capacity
1
Minimum capacity
1
Maximum capacity
3
  1. Under Scaling policies, choose Target tracking scaling policy
  • Metric type: Average CPU utilization
  • Target value: 50
  • Instance warmup: 120 seconds
  1. Skip the optional steps, review, and click Create Auto Scaling group
Console wording shifts from time to time, but the settings above are what matter. The wizard is long. The decisions in it are short.
The target value of 50 deserves a sentence. You're telling AWS "keep average CPU around 50%, and add or remove servers to hold it there." It works like a thermostat. You set the temperature and don't touch the heating yourself. Under the hood, AWS creates the CloudWatch alarms that do the work. Leave those alarms alone, because editing them by hand breaks the policy.



Step 4: Check that it works

Within a minute or two the group launches its first instance. Watch it under Auto Scaling Groups → web-asg → Activity. When the instance shows InService and the target group marks it healthy, you're ready.
Get the load balancer's address from EC2 → Load Balancers → web-alb, copy the DNS name, and open it in a browser:
http://web-alb-1234567890.us-east-1.elb.amazonaws.com
You should see "Hello from i-0abc..." with a real instance ID. If it shows a 503 error, the instance probably hasn't finished booting or passing health checks. Give it two or three minutes.



Step 5: Make it scale

A quiet server won't scale, so create some load. Find the instance's public IP under EC2 → Instances and connect with SSH, the same way as in the SSH article. Then burn the CPU:
for i in 1 2; do yes > /dev/null & done
A  t3.micro  has two vCPUs, so two  yes  processes push both to 100%. Now wait. CloudWatch looks at the metric in one-minute chunks and wants several in a row before it acts, so expect the first new instance after about three to five minutes. Refresh the Activity tab and you'll see "Launching a new EC2 instance."
Once the new server is in service, refresh the load balancer address a few times. The instance ID in the page will flip between the two servers. That's the load balancer sharing traffic.
If you want to watch from the terminal:
aws autoscaling describe-scaling-activities \
--auto-scaling-group-name web-asg \
--max-items 5
When you're done, stop the load:
pkill yes
Scaling back in is deliberately slow. Target tracking waits for about 15 minutes of low CPU before it removes a server, because shutting one down just before the next spike is worse than paying for ten extra minutes. Don't assume it's broken when nothing happens right away.
One honest warning about  t3  instances. They run in "unlimited" credit mode by default, which means sustained 100% CPU can produce a small surplus-credit charge. For a ten-minute test it's cents, but it's the reason not to leave the load running overnight.



Other kinds of scaling policy

Target tracking is the right default for a beginner. There are others for specific jobs:
Policy
How it works
Use it when
Target tracking
Holds a metric near a number
Almost always the starting point
Step scaling
Adds a different amount depending on how bad the alarm is
You need big jumps for big spikes
Scheduled scaling
Changes capacity at set times
You know when the rush comes (deadline night, a sale, 9 AM logins)
Predictive scaling
Learns daily and weekly patterns and scales ahead of time
Your traffic is regular and you have a couple of weeks of history
For the submission portal, scheduled scaling is a good fit. You know the deadline, so raise the minimum to 4 at 11 PM instead of waiting for the CPU to tell you something's wrong. It can sit alongside a target tracking policy.



Troubleshooting

What you see
Usual cause
Instances launch, then get terminated and replaced over and over
Health check fails. Nginx didn't install or the security group doesn't allow the load balancer on port 80
503 from the load balancer
No healthy targets yet, or the instances aren't registered in the target group
502 or timeout from the load balancer
The instance security group blocks port 80 from  web-alb-sg 
Group never scales out
CPU isn't above target, or the load is on a server that isn't in the group
Group never scales in
Normal. Wait about 15 minutes after the load stops
Desired capacity won't go above the maximum
Working as designed. Raise the maximum if that's what you want
Launch fails with "Invalid IAM Instance Profile" or key pair not found
The launch template refers to something that was deleted or lives in another region
The first row catches almost everyone. The group has a health check grace period (300 seconds by default) during which a new instance is allowed to fail checks while it boots. If your startup script takes longer than that, the group will kill each server before it finishes setting up. Lengthen the grace period or slim down the script.



Common mistakes

Leaving the maximum too high. Set it to what you'd be happy to pay for if everything went wrong.
Forgetting about the load balancer when cleaning up. Deleting the instances stops the EC2 charge, but the ALB keeps billing by the hour.
Putting state on the server. If a visitor uploads a file to instance A and the next request lands on instance B, the file is gone. Use S3 or a database for anything that has to survive. The storage articles later in this series cover those options.
Baking everything into user data. A script that compiles software for ten minutes will make every scale-out slow. For anything bigger than a demo, build an AMI with the software already installed.
Testing with a single subnet. A group in one Availability Zone loses everything if that zone has a problem. Two or more is the point.
Expecting instant reaction. Boot time, health checks, and warmup mean a new server takes a few minutes to help. That's why scheduled scaling exists for predictable spikes.



Clean up

Order matters here, and skipping it is how the surprise bill happens.
  1. Go to Auto Scaling Groups, select  web-asg , and click Delete. This terminates its instances too.
  1. Open EC2 → Load Balancers and delete  web-alb .
  1. Open Target Groups and delete  web-tg .
  1. Optionally delete  web-template  and the two security groups. These cost nothing, but there's no reason to keep them. Delete  web-instance-sg  first, since it refers to  web-alb-sg .
Then open Instances and make sure nothing is still running.



Quick reference

  • Auto Scaling is horizontal: more servers, not bigger ones
  • Launch template is the recipe, the group is the manager, the policy is the rule
  • Min, desired, and max. The max is your safety cap
  • Use target tracking on average CPU near 50% to start
  • Attach a load balancer and use at least two Availability Zones
  • Instances should accept traffic only from the load balancer's security group
  • Scale-out takes a few minutes. Scale-in takes about 15
  • Delete the group first, then the ALB



What comes next

Everything in this article quietly depended on something we skipped over: the VPC. The subnets, the Availability Zones, and the rule that decides which traffic can reach your servers all live inside it. The next article covers what a VPC is and why every EC2 instance sits in one.
Before then, try a scheduled action. Set the minimum to 2 for the next hour, watch the second instance appear, and then delete everything.


Group scaling out but the page still shows one instance ID? Drop a comment with what the target group says under Targets, and I'll help you find the problem.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article