top of page

Implementing AWS Auto Scaling Using EC2, ALB & Target Tracking

AWS Auto Scaling image with EC2 graphics, load balance scales, and High CPU gauge. Features text "Auto Scale & Load Balance," "CPU > 5%," and "High Availability." Cloudy backdrop.

Introduction

In modern cloud environments, auto scaling is critical for high availability, performance, and cost optimization. Instead of running multiple servers all the time, AWS allows you to automatically scale EC2 instances based on load.


In this blog, we will configure:


  1. Apache Web Server on EC2

  2. Auto Scaling Group (ASG)

  3. Launch Template

  4. Target Tracking Policy

  5. Scale out when CPU > 5%

  6. Automatically launch a second Apache instance


This setup is ideal for:

  • Web applications

  • Traffic-based scaling

  • Cost optimisation

  • High availability

Architecture Overview
Flowchart depicting user traffic through an Application Load Balancer to EC2 Instances (Apache, Auto-created) and an Auto Scaling Group with a Scaling Policy (CPU > 5%).
Prerequisites

Before starting, ensure:


  1. AWS Account

  2. IAM user with EC2, Auto Scaling, ELB access

  3. Key Pair created

  4. Basic knowledge of Linux & SSH


STEP 1: Launch First EC2 Instance (Apache Setup)


  1. Go to EC2 Dashboard → Launch Instance

AWS console screen showing EC2 launch templates. Options include Streamline provisioning and Simplify permissions. Button: Create launch template.

Setting

Value

Name

apache-asg-base

AMI

Rocky 9

Instance Type

t3.small

Key Pair

Select existing

Security Group

Allow HTTP (80) & SSH (22)

Auto-assign Public IP

Enabled

AWS console screen shows a successful creation of an EC2 instance with a green success banner. Actions log confirms tasks succeeded.

AWS EC2 console showing launch template "apache-asg-base" details. Includes version, ID, instance type t3.small, and creation date.
  1. Add User Data (Auto Install Apache)

Paste this in User Data:

#!/bin/bash
dnf update -y
dnf install httpd -y
systemctl start httpd
systemctl enable httpd

echo "<h1>Builddevops Apache Server - $(hostname)</h1>" > /var/www/html/index.html

This ensures Apache installs automatically whenever a new instance is launched.


  1. Launch the Instance

Wait until:

  • Instance is running

  • Status checks are passed

AWS EC2 launch screen showing an instance template selection with details like software image and storage. "Launch instance" button highlighted.

AWS console screenshot showing "Launch instance from template" option in action menu for apache-asg-base template. White and blue theme.

AWS console screenshot showing EC2 launch template selection. Details on software image, server type, and storage are listed.

AWS console screen showing a successful EC2 instance launch message in green. Next steps are listed below with a "View launch templates" button.

Browser test:

http://<PUBLIC-IP>

You should see:


Webpage displaying "Builddevops Apache Server - ip-172-31-73-161.ec2.internal" on a plain white background, browser toolbar visible.

STEP 2: Create an AMI from This Instance


We’ll use this AMI for auto scaling.


Steps:

  1. EC2 → Instances

  2. Select your instance

  3. Actions → Image and templates → Create Image

AWS EC2 console screenshot showing image creation form. "apache-asg-ami-builddevops" is entered as the image name.

  1. Name: apache-asg-ami

  2. Click Create Image

AWS console displays an Amazon Machine Image named "apache-asg-ami-builddevops." It shows details like AMI ID and owner, set in a dashboard view.

⏳ Wait until AMI status becomes Available


STEP 3: Create Launch Template


Why Launch Template?

Auto Scaling uses it to create new EC2 instances.


Steps:

  1. EC2 → Launch Templates

  2. Click Create Launch Template

AWS

Configuration:

Setting

Value

Name

apache-launch-template

AMI

Select your custom AMI

Instance Type

t3.micro

Key Pair

Same as earlier

Security Group

Allow HTTP & SSH

AWS

AWS

Click Create Launch Template


STEP 4: Create Target Group


  1. EC2 → Target Groups

  2. Click Create target group

AWS
  1. Choose:

Type: Instance

Protocol: HTTP

Port: 80


AWS
  1. Health check path: /

AWS
  1. Create Target Group


STEP 5: Create Application Load Balancer (ALB)


Steps:

  1. EC2 → Load Balancers → Create

AWS
  1. Select Application Load Balancer

  2. Settings:

    • Name: apache-alb

    • Scheme: Internet-facing

    • Listener: HTTP 80

AWS
  1. Select:

VPC

At least 2 subnets

Attach Target Group


AWS

Create Load Balancer


AWS

STEP 6: Create Auto Scaling Group


Go to Auto Scaling → Create ASG


AWS

Step 1: Launch Template

  • Select apache-launch-template

AWS

Step 2: Network

  • Select VPC

  • Choose minimum 2 subnets

AWS

Step 3: Attach Load Balancer

  • Select existing ALB

  • Choose Target Group

AWS

Step 4: Configure Group Size

Setting

Value

Desired

1

Minimum

1

Maximum

2

AWS

AWS

STEP 7: Configure Scaling Policy (CPU > 5%)


Choose:

✔ Target Tracking Scaling Policy

Configure:

Setting

Value

Metric

Average CPU Utilization

Target Value

5%

Instance Warm-up

60 seconds

AWS

AWS

👉 This means:

  • If CPU > 5% → new instance launches

  • If CPU < 5% → instance terminates automatically


STEP 8: Test Auto Scaling


Generate Load


SSH into the instance:

[root@siddhesh ~]# dnf install epel-release -y
[root@siddhesh ~]# dnf install stress-ng -y 
[root@siddhesh ~]# stress-ng --cpu 2 --timeout 300
AWS

What Happens?

  • CPU crosses 5%

  • Auto Scaling triggers

  • New EC2 instance is created

  • Added to Load Balancer automatically


Check:

EC2 → Auto Scaling Groups → Activity History
AWS

As shown above, a new instance was automatically launched when the instance load exceeded 5%.


AWS

After the load dropped below 5%, the newly created instance was automatically terminated by the Auto Scaling group, as shown in the screenshot above.

This confirms that the scale-in policy is working correctly and that AWS Auto Scaling is dynamically adjusting the number of running instances based on real-time CPU utilization, ensuring optimal resource usage and cost efficiency.


Best Practices


  • Use ALB instead of direct EC2 access

  • Enable CloudWatch alarms

  • Use Auto Healing

  • Use HTTPS (ACM SSL)

  • Use Launch Templates (not Launch Configurations)

  • Use IAM Role instead of access keys


Conclusion

With this setup, you have successfully:


  • Created an Apache Auto Scaling environment

  • Configured CPU-based scaling

  • Automated infrastructure growth

  • Improved availability and performance


This architecture is production-ready and scalable.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page