Building Robust Infrastructure: Achieving High Availability with AWS Auto Scaling and ALB

Sampath. V
7 min readJul 20, 2023

--

Introduction:

In today’s fast-paced digital landscape, ensuring the high availability and scalability of web applications is paramount for delivering an exceptional user experience. Cloud-based solutions have revolutionized the way we build and deploy applications, and Amazon Web Services (AWS) offers a powerful set of tools to achieve high availability. In this article, we will explore how to build a robust infrastructure using AWS Auto Scaling and Application Load Balancer (ALB) to ensure seamless performance even during traffic spikes.

What is Auto Scaling Group (ASG)?

AWS Auto Scaling Group (ASG) is a critical service provided by Amazon Web Services that enables the automatic scaling of compute resources based on application demand. With ASG, you can ensure that your application always has the right number of Amazon Elastic Compute Cloud (EC2) instances available to handle incoming traffic. This elasticity allows you to scale your infrastructure up during high-traffic periods and scale it down during periods of reduced demand, optimizing costs and performance.

What is Elastic Load Balancer (ELB)?

Elastic Load Balancer (ELB) is a fully managed load balancing service provided by AWS. It helps distribute incoming application traffic across multiple EC2 instances in a target group to ensure optimal utilization and availability of resources. ELB plays a crucial role in improving application performance, fault tolerance, and facilitating seamless scaling.

Steps to build a highly available web application with AWS Auto Scaling Group (ASG) and Elastic Load Balancer (ELB)

  1. Create an Auto Scaling Launch Template
  2. Create an Application Load Balancer (ALB) and Configure Health Checks for ALB and Instances.
  3. Create an AWS Auto Scaling Group (ASG) and Configure Scaling Policies.
  4. Test and Monitor the Infrastructure.

Step-1: Create an Auto Scaling Launch Template.

The EC2 Launch Template console allows us to create a reusable configuration template for our Amazon EC2 instances. We can specify the desired instance attributes, such as the AMI, instance type, storage, and security groups. This template simplifies the provisioning process and ensures consistency across instances launched by the AWS Auto Scaling Group.

Create an ec2 launch template

We’ll be using the Amazon Linux 2 AMI and launching instances with a t2.micro instance type.

ec2 instance ami
ec2 instance type

If you already have a key pair, you can proceed with the existing key pair. If not, create a new key pair.

ec2 key-pair

Under the Network Settings, we won’t choose a specific subnet. Instead, we’ll let the AWS Auto Scaling Group handle the selection of the appropriate subnets based on its configuration.

For the Security Group, we’ll create a new one with rules to allow HTTP/HTTPS and SSH access. This will ensure that our web application can be accessed securely, and we have the necessary SSH access to manage our instances.

ec2 security group inbound rules
choosing sg for ec2

Under the Advanced Details section paste this script into the user data

This user data script will be executed when each instance is launched, ensuring that the necessary packages are updated, the Apache HTTP server is installed, and a basic HTML file is created to serve as the content for our web server.

#!/bin/bash
# Use this for your user data (script from top to bottom)
# install httpd (Linux 2 version)
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

Congratulations! The EC2 launch template has been created successfully, and it will serve as a blueprint for our auto-scaling group to create instances as needed.

Ec2 Launch templates

Step-2: Create an Application Load Balancer (ALB) and Configure Health Checks for ALB and Instances.

Now, let’s proceed to create an Application Load Balancer (ALB) to distribute traffic to our instances.

Listeners: ALB listeners define how the load balancer receives incoming traffic. We’ll configure a listener on port 80 (HTTP) to handle regular web traffic and optionally add a listener on port 443 (HTTPS) for secure connections.

Target Groups: Target groups are used to route incoming requests from the ALB to the instances registered with the group. We’ll create a target group that includes our EC2 instances, allowing the ALB to efficiently distribute traffic to these instances based on health checks and routing rules.

Application Load Balancer (ALB)

Before proceeding to create the Application Load Balancer (ALB), please ensure you have already created the following:

  1. Security Group: Create a new security group with rules to allow HTTP/HTTPS (Ports 80 and 443) for the ALB to receive incoming traffic. Additionally, you can allow SSH (Port 22) for remote management if needed.
  2. Target Group: Create a target group to which the ALB will route incoming requests. This target group will define the instances that should receive traffic, and you can configure health checks to ensure the instances’ availability.

Once you have the security group and target group ready, you can proceed to create the ALB.

Now, let’s proceed with creating the Application Load Balancer (ALB). Follow these steps:

  1. Give your ALB a name that is descriptive and easy to identify.
  2. Ensure that the ‘Scheme’ is set to ‘Internet-facing.’ This allows the ALB to receive traffic from the internet.
  3. Under ‘Network mapping,’ select the appropriate Virtual Private Cloud (VPC) for your application. Choose the VPC where your instances are located.
  4. Select three subnets within the VPC for the ALB to distribute traffic across Availability Zones. This helps improve availability and fault tolerance.
  5. For the ‘Security groups,’ choose the previously created security group that allows HTTP/HTTPS and SSH access. This ensures that the ALB can communicate with the instances securely.
  6. For the ‘Target groups,’ select the target group that you created earlier. This will specify which instances should receive traffic from the ALB.
ALB- Target group

Congratulations! You have successfully created the Application Load Balancer (ALB).

alb-output

Step-3: Create an AWS Auto Scaling Group (ASG) and Configure Scaling Policies.

Let’s move on to creating the Auto Scaling Group (ASG) to dynamically manage the number of instances based on traffic demand.

Create — ASG

Select Name and Instance Template: Choose a name for your Auto Scaling Group and select the launch template we created earlier. This template includes all the configurations needed for the instances.

Select VPC and Subnets: Choose the Virtual Private Cloud (VPC) where your instances should be launched, and then select the subnets within the VPC that you want the Auto Scaling Group to scale across.

Choose Existing ALB and Target Group: Select the existing Application Load Balancer (ALB) you created earlier. Then, choose the target group associated with the ALB. This will ensure that the ASG automatically registers new instances with the ALB for traffic distribution.

Apply Health Checks: Define health checks to monitor the instances’ health. Instances failing health checks will be replaced automatically, ensuring high availability.

Choose Instance Scaling Limits: Specify the minimum, desired, and maximum number of instances you want to maintain within the Auto Scaling Group. This allows the ASG to automatically adjust the number of instances based on demand.

Choose Scaling Policy: Select the “Target tracking scaling policy” option for demonstration purposes. With this policy, you can set a target value for a specific metric, such as CPU utilization or request count. The ASG will then automatically adjust the number of instances to meet the target value.

Step-4 Test and Monitor the Infrastructure🕵️

To test the scalability and performance of our project, we can simulate high CPU load by using the “stress” tool. By subjecting the instances to maximum stress, we can observe how AWS Auto Scaling responds to increased demand, ensuring our infrastructure scales dynamically and efficiently to meet the workload.

To prepare for the CPU stress test, we need to install the “stress” tool using the following commands.

Once “stress” is installed, you can run the following command to load the CPU to the maximum with four threads.

stress -c 4

After running the stress test, you can observe and analyze the performance changes by checking the following AWS monitoring services: CloudWatch, Auto Scaling Group Activity History, EC2 Instance Monitoring

Finally, cross-check the Application Load Balancer (ALB) DNS to verify whether it successfully distributed the traffic across the instances during the stress test.

Success! With AWS Auto Scaling, Application Load Balancer, and proper monitoring, we have built a highly available and scalable web application. Thank you 🙏 for joining this journey to optimize performance and ensure seamless user experiences in the cloud!

To stay updated with AWS insights and tech articles, follow me on LinkedIn 👉 Sampath-Vijayan. Happy scaling and looking forward to connecting with you there!

--

--