Creating an Application Load Balancer to Distribute Traffic Across Two Instances on AWS

Emmanuel
4 min readJun 24, 2024

--

In modern web applications, ensuring traffic is effectively managed and evenly distributed across multiple instances is crucial for maintaining high availability and performance.

AWS provides an efficient way to achieve this using an application load balancer (ALB). This article will guide you through creating an ALB to distribute traffic between two instances using the same Ubuntu AMI. We will also utilize user data to configure these instances automatically.

Prerequisites

Before we begin, ensure you have the following:

  1. An AWS account.
  2. AWS CLI is configured on your local machine.
  3. Basic knowledge of EC2, Security Groups, and VPC.

Step 1: Launch Two EC2 Instances

First, launch two EC2 instances using the same Ubuntu AMI.

You can configure your instances at launch using the following user data script.

This script updates the package list, installs Apache, and starts the NGINX server.

#!/bin/bash
sudo apt update -y
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo sudo tee /var/www/html/index.html > /dev/null <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Hello from T2S on Server 1!</title>
</head>
<body>
<h1>Hello from T2S on Server 1!</h1>
</body>
</html>
EOF

Here are steps to help you launch these two instances:

Log in to the AWS Management Console. Navigate to the EC2 Dashboard.

Click on “Launch Instance.”

Choose an Amazon Machine Image (AMI). For this example, select an Ubuntu AMI.

Choose an instance type (e.g., t2.micro for testing).

In the “Configure Instance” step, paste the above user data script into the “User data” field.

Add any necessary storage.

Configure a Security Group to allow HTTP (port 80) traffic and SSH to allow traffic on port 22.

Review and launch the instance.

Repeat the steps to launch a second instance (name it elb-server2). But this time, use this script in the User data section:

#!/bin/bash
sudo apt update -y
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo sudo tee /var/www/html/index.html > /dev/null <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Hello from T2S on Server 2!</title>
</head>
<body>
<h1>Hello from T2S on Server 2!</h1>
</body>
</html>
EOF

Step 2: Create an Application Load Balancer

Next, create an Application Load Balancer to distribute traffic across the two instances.

Navigate to the EC2 Dashboard:

  • In the left-hand menu, under “Load Balancing,” click “Load Balancers.”
  • Click “Create Load Balancer.”

Select the Load Balancer Type:

  • Choose “Application Load Balancer.”
  • Click “Create.”

Configure the Load Balancer:

  • Name your load balancer (e.g., elb-server).
  • Select the scheme as “internet-facing” for a publicly accessible load balancer.
  • Choose the appropriate VPC and at least two subnets in different Availability Zones for high availability.
  • Under “Listeners,” ensure the default listener on port 80 is present.

Ensure your Target Group, the resource you will create next, is in the same Availability Zone as your Load Balancer.

Configure Security Settings:

  • Choose or create a security group that allows HTTP (port 80) traffic.

Configure Routing:

  • Create a new target group (e.g., elb-server-tg).
  • Set the target type to “instance.”
  • Choose the protocol as HTTP and port 80.
  • Health checks can be left as default (HTTP and path /).

Register Targets:

  • Select the two instances you launched earlier and add them to the target group.
  • Click “Next: Review” and then “Create.”

If all is done well, you should see something like this:

The Health status should be healthy if all targets have been registered correctly.

You can also accomplish the above using the AWS CLI. Get the commands from GitHub Repo at

Step 3: Test Your Load Balancer

After creating the load balancer, it will take a few minutes to become active. Once active, you can test it by accessing the DNS name of the load balancer.

Find the DNS Name:

  • Navigate to the EC2 Dashboard.
  • Under “Load Balancers,” select your newly created load balancer.
  • Copy the DNS name from the load balancer details.

Access the Load Balancer:

  • Open a web browser and paste the DNS name into the address bar.
  • You should see a message from one of your instances, “Hello from T2S on Server 1,” changing to “Hello from T2S on Server 2,” as you refresh your internet browser.

By refreshing the page, you should see the message alternate between the two instances, indicating that traffic is being distributed by the load balancer.

Conclusion

Creating an Application Load Balancer to distribute traffic between two instances on AWS is a straightforward process that significantly enhances the reliability and scalability of your web applications.

Following the steps outlined in this article will allow your application to remain available and perform well even under varying load conditions. Happy provisioning, and God bless you!

--

--

Emmanuel
Emmanuel

Written by Emmanuel

Certified Cloud Solutions Architect passionate about Cloud & DevOps, Technology Evangelism, Pastoral Ministry, Educator and Mentor, and World Travel.

No responses yet