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:

Core Concepts

Before diving into the setup, let’s clarify some key terms:

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:

  1. Go to the EC2 Dashboard in the AWS Management Console.
  2. Select Launch Templates from the left-hand menu and click Create Launch Template.
  3. Provide a name and description for the template.
  4. Under Application and OS Images (AMI), select Amazon Linux 2 AMI.
  5. Choose t2.micro as the instance type.
  6. Configure the Security Group to allow HTTP traffic (port 80).
  7. 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:

  1. Go to the Launch Templates section in the EC2 Dashboard.
  2. Locate the template you created and click Actions > Launch Instance from Template.
  3. Choose the number of instances to launch (e.g., 2).
  4. Select the appropriate subnet and security group.
  5. 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:

  1. Go to the EC2 Dashboard and select Load Balancers from the left-hand menu.
  2. Click on Create Load Balancer and choose Application Load Balancer.
  3. Provide a name for your load balancer and select Internet-facing as the scheme.
  4. Choose the appropriate Availability Zones.
  5. Configure the Security Group to allow HTTP traffic (port 80).
  6. Set up a Listener for HTTP traffic on port 80.
  7. Create a Target Group and register the EC2 instances as targets.
  8. Complete the ALB setup and wait for provisioning.

Test the Load Balancer

To verify that the load balancer is working correctly:

  1. Open a web browser and paste the DNS name of the ALB into the address bar.
  2. Refresh the page multiple times.
  3. 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.