Site icon GoGoSoon

AWS Fundamentals – A Beginner’s Guide to Application Load Balancers

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.

In this article, we’ll see about Application Load balancers and how to implement them. Before we start let’s understand a bit about load balancer.

What is Load Balancer

Quoting from official AWS documentation

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 cool 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.

Benefits of using Load Balancer

Some of the benefits are

What is 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 "

Hello World from $(hostname -f)

" > /var/www/html/index.html

EC2 user data

6. Launch two instances, providing distinct names for each (e.g., “My First Instance” and “My Second Instance”).

EC2 instances

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 “Hello World from [your first instance ip address]”
  2. Repeat the same for second instance you can see “Hello World from [your second instance ip address]”
My First Instance
My Second Instance

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., “DemoALB”).
  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

Listeners and Routing for ALB

Step 4: Creating a Target Group

  1. Choose a target type as Instances
  2. Name the target group (e.g., “demo-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
Target Group Configuration
Register EC2 instances

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
Link Target Group to ALB

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.
Active Load Balancer

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.

Bonus 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

Congratulations! You’ve successfully set up an Application Load Balancer on AWS. By following these steps, you’ve learned how to create a reliable and efficient environment for your applications, ensuring they stay responsive and available even when one instance experiences issues.

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

Hope you learned something new. If you wish to learn more about AWS, subscribe to my newsletter by entering your email address in the below box.

Have a look at my site which has a consolidated list of all my blogs.

Exit mobile version