Beginner’s Guide to AWS Auto Scaling Group with Load Balancer

In this guide, we'll walk through setting up an Auto Scaling Group (ASG) and integrating it with a Load Balancer on AWS. Auto Scaling allows your application to automatically scale in or out based on demand, while a Load Balancer distributes incoming traffic across multiple instances to ensure higher availability and reliability. By the end of this article, you’ll have a simple web application running on EC2 instances behind an Auto Scaling Group and Load Balancer.
Prerequisites
AWS Account
Basic understanding of AWS services like EC2, Load Balancers, and Auto Scaling.
AWS CLI is installed and configured on your local machine.
Steps Overview:
Create a Launch Template that defines the EC2 instances configuration.
Create an Auto Scaling Group (ASG).
Set up a Load Balancer.
Attach the Load Balancer to the Auto Scaling Group.
Test the setup.
Let’s start by creating the EC2 instances with a simple web server using a bash script.
Step 1: Create a Launch Template
Log in to AWS Management Console.
Go to the EC2 Dashboard.
Under the Instances section, click on Launch Templates.
Click Create Launch Template.
Launch Template Name:
web-server-templateAmazon Machine Image (AMI): Choose an Amazon Linux 2 AMI.
Instance Type: Select
t2.micro(this is Free Tier eligible).Key Pair: Choose an existing key pair or create a new one. This is required to SSH into your instances.
Security Groups: Here’s where you need to configure the security group to allow specific inbound traffic, including SSH, HTTP, HTTPS, and ICMP (ping).
Security Groups Configuration:
Create or select a Security Group that allows the following:
SSH (Port 22): This allows you to securely connect to your instance via SSH from your local machine. Make sure to restrict access to your IP or a range of trusted IPs for security purposes.
HTTP (Port 80): This allows web traffic to reach your instance, enabling it to serve the website via the Apache server.
HTTPS (Port 443): This allows secure web traffic (HTTPS). You would enable this if you plan to serve content over HTTPS using SSL certificates in the future.
All ICMP - IPv4: This allows ping (ICMP) traffic, which is useful for troubleshooting and ensuring the instance is reachable from your network.
You can create a new security group as follows:
In the Security Group creation screen:
Security Group Name:
web-server-sgDescription:
Allow SSH, HTTP, HTTPS, and ICMP trafficInbound Rules:
SSH (Port 22): Source:
My IP(or specify your public IP for secure SSH access).HTTP (Port 80): Source:
Anywhere(0.0.0.0/0), allowing public HTTP access.HTTPS (Port 443): Source:
Anywhere(0.0.0.0/0), allowing public HTTPS access.All ICMP - IPv4: Source:
Anywhere(0.0.0.0/0), to allow ping requests.
Outbound Rules: Leave it as default, which allows all outbound traffic.
After setting up the security group, assign it to your launch template.
User Data: Copy and paste the following script into the User Data field. This script installs Apache and creates a simple HTML page:
bashCopy code#!/bin/bash sudo yum update -y sudo yum install httpd -y sudo systemctl start httpd sudo systemctl enable httpd echo "<html><head> <title>Sample Page</title> </head><body> <h1>Welcome to my website </h1><p>This is a sample HTML page served by Apache on $(hostname).</p> </body></html>"> /var/www/html/index.htmlClick Create Launch Template to finalize your settings.


Explanation of Security Group Settings:
SSH (Port 22): This rule allows you to connect to your instance via SSH. Be sure to limit SSH access to your specific IP address to reduce the risk of unauthorized access.
HTTP (Port 80): This rule allows incoming web traffic over HTTP. It's essential for making your web server accessible to the public over the internet.
HTTPS (Port 443): This rule allows secure web traffic using SSL certificates. Even if you're not serving secure content yet, it's a good idea to allow this traffic for future upgrades.
ICMP (ping): This allows your instance to respond to ping requests. This is useful for diagnosing network issues and ensuring your instance is reachable from other networks.
By configuring these security group rules, your instance will be ready to handle traffic from web users, while allowing administrative SSH access for management and monitoring.
Step 2: Create an Auto Scaling Group
Next, we’ll create an Auto Scaling Group that automatically scales the number of EC2 instances based on the demand.
In the EC2 Dashboard, click Auto Scaling Groups under the Auto Scaling section.
Click Create Auto Scaling Group.
Auto Scaling Group Name:
web-server-asgLaunch Template: Select the
web-server-templatewe created earlier.VPC and Subnets: Select the VPC and the subnets where your instances should run.
Configure Scaling Policies: You can choose to scale based on average CPU utilization or other metrics. For simplicity, choose to Create a Target Tracking Scaling Policy with CPU Utilization at 50%.
Desired Capacity: Set this to 2 (minimum 2, maximum 4) to ensure we always have two instances running.
Click Create Auto Scaling Group.
Step 3: Set up an Elastic Load Balancer
An Elastic Load Balancer (ELB) will distribute incoming traffic across the EC2 instances in the Auto Scaling Group.
In the EC2 Dashboard, click Load Balancers under the Load Balancing section.
Click Create Load Balancer and choose Application Load Balancer.
Load Balancer Name:
web-app-lbScheme: Choose
Internet-facing.Listeners: Keep HTTP as the default listener on port 80.
VPC and Subnets: Select the same VPC and subnets as your Auto Scaling Group.
Click Next: Configure Security Settings, and skip this step since we’re using HTTP.
Configure Security Group: Use the security group that allows HTTP access on port 80.
Configure Routing:
Target Group: Create a new target group.
Target Group Name:
web-server-target-groupTarget Type: Instance
Health Check Path:
/(the default).
Review your settings and click Create Load Balancer.
Step 4: Attach the Load Balancer to the Auto Scaling Group
Now we’ll configure the Auto Scaling Group to route traffic through the Load Balancer.
Go back to your Auto Scaling Group.
Under Load Balancing, click Edit.
Select the Application Load Balancer we created (
web-app-lb).Choose the Target Group (
web-server-target-group).Save the changes.
Step 5: Test the Setup
Once everything is set up:
Navigate to the Load Balancer section in the AWS Management Console.
Copy the DNS name of your Load Balancer.
Paste the DNS name into your browser, and you should see the web page served by your EC2 instances:
This confirms that your Auto Scaling Group is working correctly, and the Load Balancer is distributing traffic to the EC2 instances.
Conclusion
In this tutorial, we set up an Auto Scaling Group with a Load Balancer in AWS, using a simple web server application on EC2 instances. This setup provides automatic scaling and high availability, ensuring your application can handle increased traffic efficiently.

