How to Create an AWS Application Load Balancer for Your EC2 Instances

Nipulpatel
7 min readSep 1, 2023

--

Load balancers are servers that forward internet traffic to multiple servers (EC2 Instances) downstream. If one of your servers is down or unhealthy, the load balancer helps to direct traffic to the available server to ensure no downtime. Load balancers ensure organisations can maintain the performance and availability of their applications which helps them stay a step ahead of the competition. load balancing services, which help distribute traffic across multiple servers and improve application performance and availability.

Imagine you run a popular e-commerce website that experiences a significant increase in traffic during holiday sales or special promotions. Your servers might get overwhelmed by the sudden surge in visitors, leading to slow page loading or even server crashes. This could result in frustrated customers abandoning their shopping carts and a loss of revenue.

To avoid this situation one can use load balancers. AWS offers various load balancers like Application Load Balancer(ALB), Network Load Balancer(NLB), and Gateway Load Balancer.

What is a Load Balancer?

A load balancer is a service that helps distribute incoming network traffic across multiple servers or instances. By doing so, load balancers help optimize the performance and availability of web applications. A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.

In simple words, Load Balancers are servers that forward traffic to available server.

for example, Assume we have 3 servers (EC2 instances) fronted by a load balancer. Now think like 3 users directly connecting to the load balancer. The load balancer decides which server should handle each user’s request.

Let’s say the first user arrives. The load balancer sends their request to one server, which we’ll call “Server A.” Then, another user comes in, and the load balancer sends their request to a different server, “Server B.” Finally, a third user arrives, and their request is sent to “Server C.”

This way, no one server gets too busy, and everything stays fast and responsive.

The good part is that the users do not know which backend instances they’re connected to. They just know that they have to connect to your load balancer, which gives them one endpoint of connectivity only.

In the above diagram, it can be seen that Server 1 is overloaded without a load balancer.

In order to utilize all the servers efficiently, we need to distribute a load of incoming requests among them. And this process of distributing incoming network traffic

across a group of backend servers is called Load Balancing.

Why use a load balancer?

  1. Spread load across multiple downstream instances
    2.Expose a single point of access(DNS) to your application
    3.Seamlessly handle failures of downstream instances
    4.Do regular health checks to your instances
    5.Provide SSL termination (HTTPs) for your website
    6.High availability across zones

We will be focusing on Application Load Balancer

Application Load Balancers operate at the OSI layer 7 (the application layer) and are designed to route traffic based on application-specific content. They are well-suited for handling HTTP/HTTPS traffic and can intelligently distribute requests based on URL, hostname, or cookies. ALBs are ideal for scenarios where multiple services or microservices are hosted on the same set of servers.

Hands-On: Implementing an Application Load Balancer

Now that we have understood about load balancers, let’s get our hands on implementing an Application Load Balancer in AWS.

Step 1: Launching EC2 Instances

1. Log in to your AWS Management Console.

2. Navigate to the EC2 Dashboard.

3. Click on “Launch Instances” to start creating new EC2 instances.

4. Configure the instance settings, such as instance type (e.g., t2.micro), Amazon Linux image, and security groups (allow ssh,http and https traffic)

5. In the Advanced Details section, paste the code in the User data input.

#!/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>this is server 1$(hostname -f)</h1>" > /var/www/html/index.html

6. Launch two instances, providing distinct names for each (e.g., “server 1” and “server 2”).

Step 2: Accessing Instances

  1. After launching the instances, copy the IPv4 address of the first instance. Open a web browser and paste the IPv4 address you can see the message “Hi this is server 1[your first instance ip address]”
  2. Repeat the same for second instance you can see “Hi this is server 2 instance ip address]”

Step 3: Creating an Application Load Balancer

  1. Navigate back to the EC2 Dashboard.
  2. Scroll down to “Load Balancing” and choose “Load Balancers.”
  3. Click “Create Load Balancer.”
  4. Select “Application Load Balancer.”
  5. Name your ALB (e.g., “testALB”).
  6. Choose an internet-facing scheme and IPv4 address type.
  7. In Network Mappings select atleast 2 availabilty zones. I recommend to select all the avaliability zones because the load balancer will send traffic only to selected availability zones
  8. For security group, remove the default, create new security group and allow only “HTTP” (port 80) traffic

9. Define listeners and routing — Route traffic from HTTP on port 80 to a target group. Click on “Create target group” to create a new target group

Step 4: Creating a Target Group

  1. Choose a target type as Instances
  2. Name the target group (e.g., “test-tg-alb”).
  3. Choose “HTTP” for protocol and port 80.
  4. Register both EC2 instances you created in the first step by selecting the both instances and clicking on “Include as pending below” button

Step 5: Linking Target Group to ALB

  1. Go back to the ALB configuration
  2. Under “Listeners,” associate the ALB with the target group you created

Step 6: Launching the ALB

  1. Complete the ALB creation process by clicking on “Create load balancer” button at the bottom of the page
  2. Wait for the ALB to become active and obtain its DNS name.

Step 7: Testing the Load Balancer

  1. Copy the DNS name of your ALB.
  2. Paste the DNS name in a new web browser tab.
  3. Refresh the page multiple times to see the load balancing in action, distributing traffic between the instances.

additional point

Now you can access the application both by using the EC2 IPv4 address or DNS of the load balancer. But its not recommended to allow others to hit your EC2 instances directly. If the EC2 is open hackers can easily hack your server.

To avoid this situation edit the security group of your EC2 instances delete the HTTP rule and add rule allow HTTP only from the security group (select the security group you created for the Load Balancer) and save the rule

Now if you hit the IPv4 address of the EC2 instances the page will not be loaded

You can access the server only using the DNS of the application load balancer

Conclusion

AWS’s Load Balancer takes care of distributing traffic seamlessly, maintaining the health of your instances, and improving the overall performance of your applications.

--

--

No responses yet