Setting Up an AWS Application Load Balancer (ALB)
In this guide, we’ll walk through the process of setting up an AWS Application Load Balancer (ALB) to distribute traffic across multiple EC2 instances. The ALB operates at Layer 7 of the OSI model, making it ideal for routing HTTP/HTTPS traffic. By the end of this tutorial, you’ll have a fully functional ALB distributing traffic between EC2 instances, ensuring high availability and scalability for your applications.
Here’s what we’ll cover:
- Creating EC2 instances using a Launch Template.
- Setting up an Application Load Balancer (ALB).
- Configuring a Target Group and Listener.
- Testing the ALB to ensure traffic is distributed evenly.
Core Concepts
Before diving into the setup, let’s clarify some key terms:
- DNS Name: The DNS name is a human-readable address assigned to your ALB. It resolves to the IP address of the load balancer and allows users or applications to access it without needing to know the underlying IP addresses, which can change dynamically.
- Target Group: A target group is a logical grouping of backend resources (e.g., EC2 instances, containers, or Lambda functions) that receive traffic from the load balancer. The ALB routes incoming traffic to the targets in the group based on the rules you configure.
- Listener: A listener is a process that checks for incoming connection requests from clients. It listens on a specific port (e.g., port 80 for HTTP or port 443 for HTTPS) and forwards traffic to the appropriate target group based on rules you define.
- Health Check: A health check is a mechanism used by the ALB to monitor the status of targets in a target group. If a target fails the health check, the ALB stops routing traffic to it until it becomes healthy again.
- Availability Zones: Availability Zones (AZs) are isolated data centers within an AWS region. They provide fault tolerance and high availability by distributing traffic across multiple zones.
- Security Group: A security group acts as a virtual firewall that controls inbound and outbound traffic for your resources. It ensures only legitimate traffic reaches your application.
Create a Launch Template
A Launch Template simplifies the process of launching EC2 instances by predefining configurations such as the AMI, instance type, and user data. Follow these steps to create a Launch Template:
- Go to the EC2 Dashboard in the AWS Management Console.
- Select Launch Templates from the left-hand menu and click Create Launch Template.
- Provide a name and description for the template.
- Under Application and OS Images (AMI), select Amazon Linux 2 AMI.
- Choose t2.micro as the instance type.
- Configure the Security Group to allow HTTP traffic (port 80).
- In the User Data section, paste the following script to install and configure a basic web server:
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "My hostname is $(hostname)" > /var/www/html/index.html
This script updates the system, installs the Apache web server, starts the service, and creates a simple HTML page displaying the hostname of the instance.
Launch EC2 Instances
Now that the Launch Template is ready, you can use it to launch EC2 instances:
- Go to the Launch Templates section in the EC2 Dashboard.
- Locate the template you created and click Actions > Launch Instance from Template.
- Choose the number of instances to launch (e.g., 2).
- Select the appropriate subnet and security group.
- Click Launch Instance.
Once the instances are launched, verify that they are running and have the web server configured correctly.
Create an Application Load Balancer
With the EC2 instances up and running, the next step is to create an Application Load Balancer to distribute traffic between them:
- Go to the EC2 Dashboard and select Load Balancers from the left-hand menu.
- Click on Create Load Balancer and choose Application Load Balancer.
- Provide a name for your load balancer and select Internet-facing as the scheme.
- Choose the appropriate Availability Zones.
- Configure the Security Group to allow HTTP traffic (port 80).
- Set up a Listener for HTTP traffic on port 80.
- Create a Target Group and register the EC2 instances as targets.
- Complete the ALB setup and wait for provisioning.
Test the Load Balancer
To verify that the load balancer is working correctly:
- Open a web browser and paste the DNS name of the ALB into the address bar.
- Refresh the page multiple times.
- Observe that the displayed hostname changes between the two EC2 instances, indicating that the load balancer is distributing traffic evenly.
Conclusion
By following this guide, you’ve successfully set up an AWS Application Load Balancer to distribute traffic between EC2 instances launched using a Launch Template. This setup ensures high availability and scalability for your applications.