DEV Community

Muhammad Nadeem
Muhammad Nadeem

Posted on

Build a Production-Ready Auto Scaling Infrastructure on AWS

**🚀 Introduction

**
Auto Scaling is one of the most powerful features of AWS, but many beginners find it confusing. In this hands-on lab, you'll build a complete auto-scaling infrastructure from scratch and watch servers automatically scale up and down based on demand.

By the end of this guide, you'll understand:

  1. How EC2 Auto Scaling Groups work
  2. Load balancing and traffic distribution
  3. CloudWatch metrics and alarms
  4. Real-world scaling in action

**

🏗️ Architecture We're Building

**┌─────────────────────────────────────────────────┐
│ User Traffic (Internet) │
└────────────────────┬────────────────────────────┘


┌────────────────────────┐
│ Application Load │
│ Balancer (ALB) │
└─────────┬──────────────┘

┌────────┴────────┐
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ Server │ │ Server │
│ #1 │ │ #2 │
│ (Port80)│ │ (Port80)│
└─────────┘ └─────────┘
│ │
└────────┬────────┘

Auto Scaling Group
(Manages server count)

CloudWatch Monitoring
(Triggers scaling rules)

**

Step 1️⃣: Launch Your First EC2 Instance

**
This will be the template for our auto-scaling servers.

1.1 Open AWS Console

Navigate to: https://console.aws.amazon.com → EC2 Dashboard

1.2 Launch an Instance
EC2 → Instances → Launch Instances
1.3 Configure Instance Details

Fill in these settings:

Setting Value
Name MyWebServer
AMI Amazon Linux 2 (Free tier)
Instance Type t2.micro
Key Pair Create new: my-keypair.pem
Security Group Allow SSH (22) + HTTP (80)
Storage 8 GB (default)
1.4 Add Startup Script

In Advanced Details → User Data, paste this script:

bash

!/bin/bash

yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd

Create simple website

echo "

Server: $(hostname -f)

" > /var/www/html/index.html
echo "

CPU Load: $(uptime)

" >> /var/www/html/index.html

What this does:

Updates system packages
Installs Apache web server (httpd)
Creates a simple HTML page showing server info
1.5 Launch & Verify
Click Launch Instance
Wait 2-3 minutes for it to boot
Note the Public IPv4 address
Open in browser:http://54.245.76.10
_**

Step 2️⃣: Create a Launch Template

**_
A Launch Template is like a blueprint that Auto Scaling will use to create new servers.

2.1 Navigate to Launch Templates
EC2 → Launch Templates → Create Launch Template
2.2 Fill Template Details
Name: MyWebServerTemplate
Description: Web server with auto-startup
AMI: Amazon Linux 2
Instance Type: t2.micro
Key Pair: my-keypair.pem
Security Group: Allow HTTP + SSH
2.3 Add User Data

Paste the same startup script from Step 1.4:

bash

!/bin/bash

yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "

Server: $(hostname -f)

" > /var/www/html/index.html
echo "

CPU Load: $(uptime)

" >> /var/www/html/index.html
2.4 Create Template

Click Create Launch Template ✅

What we accomplished:

Template created that can spawn servers automatically
All servers will have identical configuration

**

Step 3️⃣: Create a Target Group

**
Target Groups tell the Load Balancer which servers to send traffic to.

3.1 Navigate to Target Groups
EC2 → Target Groups → Create Target Group
3.2 Configure Target Group
Name: MyWebServers
Protocol: HTTP
Port: 80
VPC: Default
3.3 Health Check Settings

This is crucial - it monitors if servers are healthy:

Protocol: HTTP
Path: /
Matcher: 200
Interval: 30 seconds
Timeout: 5 seconds
Healthy threshold: 2
Unhealthy threshold: 3

Explanation:

Every 30 seconds, check if server responds with HTTP 200
If 2 checks pass → server is healthy
If 3 checks fail → server is unhealthy (remove from rotation)
3.4 Create Target Group

Click Create Target Group ✅

What we accomplished:

Created health monitoring for our servers
Load Balancer will use this to route traffic

**

Step 4️⃣: Create Application Load Balancer
**
The Load Balancer distributes traffic across multiple servers.

4.1 Navigate to Load Balancers
EC2 → Load Balancers → Create Load Balancer
4.2 Select Type
Type: Application Load Balancer (ALB)
4.3 Basic Configuration
Name: MyALB
Scheme: Internet-facing
IP Type: IPv4
4.4 Network Configuration
VPC: Default VPC
Subnets: Select ALL (minimum 2)
4.5 Security Group
Allow: HTTP (port 80) from 0.0.0.0/0
4.6 Listener Configuration
Protocol: HTTP
Port: 80
Forward to: MyWebServers (Target Group)

**

Step 5️⃣: Create Auto Scaling Group (The Main Event!)

**
This is where the magic happens!

5.1 Navigate to Auto Scaling Groups
EC2 → Auto Scaling Groups → Create Auto Scaling Group
5.2 Select Launch Template
Name: MyWebServers-ASG
Launch Template: MyWebServerTemplate
Version: Latest
5.3 Network Configuration
VPC: Default
Subnets: Select ALL (minimum 2 different AZs)
5.4 Load Balancer Settings
✅ Enable load balancing
Target Groups: MyWebServers
5.5 Group Size (IMPORTANT!)
Min Size: 1
Desired Capacity: 2
Max Size: 4

What this means:

Always keep minimum 1 server (no downtime)
Normally run 2 servers (desired state)
Never exceed 4 servers (cost control)
5.6 Scaling Policies

Select: Target Tracking Scaling Policy

Scale UP Rule:
Policy Name: ScaleUp
Metric: Average CPU Utilization
Target Value: 50%
Instance Warmup: 300 seconds

Meaning: If average CPU > 50%, launch more servers

Scale DOWN Rule:
Policy Name: ScaleDown
Metric: Average CPU Utilization
Target Value: 20%

Meaning: If average CPU < 20%, remove servers

5.7 Instance Maintenance
Select: Mixed behavior (No policy)
Healthy percentage: Min 90%, Max 110%

**

Step 6️⃣: Verify Everything is Running

**6.1 Check Instances
EC2 → Instances

✅ Should see 2 instances launched automatically

6.2 Check Load Balancer Health
EC2 → Load Balancers → MyALB

Note the DNS name, open in browser:

http://MyALB-xxxx.us-west-2.elb.amazonaws.com

✅ Should see your web page

Refresh multiple times - you might see different servers responding!

6.3 Check Auto Scaling Group Status
EC2 → Auto Scaling Groups → MyWebServers-ASG
Activity tab
**

Step 7️⃣: Simulate Traffic & Watch It Scale! 🎯

**
This is the most exciting part!

7.1 Connect to an Instance via SSH
bash

First, make sure your key has correct permissions

chmod 400 ~/Downloads/my-keypair.pem

Connect to instance

ssh -i ~/Downloads/my-keypair.pem ec2-user@54.245.76.10

Or use EC2 Instance Connect in AWS Console (easier!)

7.2 Simulate High CPU Load

Once connected, run:

bash

Create CPU load

yes > /dev/null &
yes > /dev/null &
yes > /dev/null &

⏱️ Watch the Magic Happen:

Open CloudWatch → Dashboards
Monitor CPU Utilization metric
Wait 2-3 minutes...

What you'll see:

CPU usage climbs above 50% 📈
CloudWatch triggers scaling policy
A new server automatically launches! 🚀
Instance count changes from 2 → 3
Time 0:00 → 2 servers (normal)
Time 2:00 → CPU spikes (>50%)
Time 3:00 → CloudWatch detects high CPU
Time 4:00 → Scaling policy triggered
Time 5:00 → New server launched! 3rd instance appears
Time 6:00 → Load distributes across 3 servers
7.3 Watch It Scale Down
bash

Kill the load generators

killall yes

⏳ Wait 5 minutes...

CPU drops below 20%
CloudWatch triggers scale-down policy
Extra server automatically terminates! ✂️

**

Step 8️⃣: Monitor Everything in CloudWatch

**8.1 Open CloudWatch
AWS Console → CloudWatch → Dashboards
8.2 Create Custom Dashboard (Optional)

Track these metrics:

CPU Utilization (average across ASG)
Network In/Out
Desired/Running instance count
8.3 Check Auto Scaling Activity
EC2 → Auto Scaling Groups → MyWebServers-ASG → Activity

See all scaling events in log:

2024-08-13 16:30:00 | Launching 1 new instance
2024-08-13 16:35:00 | Terminating 1 instance

THUMBNAIL OF A LAB

Uploading image

Top comments (0)