DEV Community

Cover image for Day 44: Implementing Auto Scaling for High Availability in AWS
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

Day 44: Implementing Auto Scaling for High Availability in AWS

Lab Information

The DevOps team is tasked with setting up a highly available web application using AWS. To achieve this, they plan to use an Auto Scaling Group (ASG) to ensure that the required number of EC2 instances are always running, and an Application Load Balancer (ALB) to distribute traffic across these instances. The goal of this task is to set up an ASG that automatically scales EC2 instances based on CPU utilization, and an ALB that directs incoming traffic to the instances. The EC2 instances should have Nginx installed and running to serve web traffic.

Create an EC2 launch template named xfusion-launch-template that specifies the configuration for the EC2 instances, including the Amazon Linux 2 AMI, t2.micro instance type, and a security group that allows HTTP traffic on port 80.
Add a User Data script to the launch template to install Nginx on the EC2 instances when they are launched. The script should install Nginx, start the Nginx service, and enable it to start on boot.
Create an Auto Scaling Group named xfusion-asg that uses the launch template and ensures a minimum of 1 instance, desired capacity is 1 instance and a maximum of 2 instances are running based on CPU utilization. Set the target CPU utilization to 50%.
Create a target group named xfusion-tg, an Application Load Balancer named xfusion-alb and configure it to listen on port 80. Ensure the ALB is associated with the Auto Scaling Group and distributes traffic across the instances.
Configure health checks on the ALB to ensure it routes traffic only to healthy instances.
Verify that the ALB's DNS name is accessible and that it displays the default Nginx page served by the EC2 instances.

Lab Solutions

Step 1: Create Security Group for EC2 & ALB

Go to EC2 → Security Groups

Click Create security group

Configuration

Security group name: xfusion-sg

VPC: Default VPC

Inbound Rules

Add:

Type: HTTP
Port: 80
Source: 0.0.0.0/0

Leave outbound rules as default

Click Create security group

Step 2: Create EC2 Launch Template

Go to EC2 → Launch Templates

Click Create launch template

Basic Details

Launch template name:

xfusion-launch-template

AMI & Instance Type

AMI: Amazon Linux 2

Instance type: t2.micro

Network & Security

Security group: xfusion-sg

User Data (IMPORTANT)

Under Advanced details → User data, paste:

#!/bin/bash
dnf update -y
dnf install -y nginx
systemctl start nginx
systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Click Create launch template

✅ Launch template is ready

Step 3: Create Target Group

Go to EC2 → Target Groups

Click Create target group

Configuration

Target type: Instances

Target group name:

xfusion-tg

Protocol: HTTP

Port: 80

VPC: Default VPC

Health Check

Protocol: HTTP

Path: /

Click Create target group

Step 4: Create Application Load Balancer

Go to EC2 → Load Balancers

Click Create load balancer

Choose Application Load Balancer

Basic Configuration

Name:

xfusion-alb

Scheme: Internet-facing

IP address type: IPv4

Network Mapping

VPC: Default VPC

Subnets: Select at least two AZs

Security Group

Select xfusion-sg

Listener & Routing

Listener:

HTTP : 80

Forward to:

xfusion-tg

Click Create load balancer

Step 5: Create Auto Scaling Group

Go to EC2 → Auto Scaling Groups

Click Create Auto Scaling group

Step 5.1: Choose Launch Template

Launch template: xfusion-launch-template

Version: Latest

Click Next

Step 5.2: Network

VPC: Default VPC

Subnets: Same AZs as ALB

Click Next

Step 5.3: Load Balancing

Select Attach to an existing load balancer

Choose:

xfusion-tg

Enable ELB health checks

Click Next

Step 5.4: Group Size & Scaling

Minimum capacity: 1

Desired capacity: 1

Maximum capacity: 2

Step 5.5: Scaling Policy

Select Target tracking scaling policy

Metric: Average CPU utilization

Target value:

50

Click Next → Create Auto Scaling group

✅ ASG is now active

Step 6: Verify Health Checks

Go to EC2 → Target Groups → xfusion-tg

Open Targets tab

Status should be:

Healthy

Step 7: Verify Application Access via ALB

Go to EC2 → Load Balancers

Select xfusion-alb

Copy DNS name

Open browser:

http://


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)