How to Set Up an Application Load Balancer with AWS EC2: Day 41 of 90 Days of DevOps

A Step-by-Step Guide to Setting Up an Application Load Balancer with AWS EC2

Ajit Fawade
10 min readNov 7, 2023

Are you a DevOps learner who wants to set up an application load balancer with AWS EC2? If yes, then this blog post is for you.

In this post, I’ll explain what load balancing is and how to use Elastic Load Balancing (ELB) in AWS EC2, a service that automatically distributes incoming traffic across multiple EC2 instances.

By the end of this post, you’ll be able to:

  • Understand what load balancing is and why it is important for your applications
  • Understand what ELB is and how it works in AWS EC2
  • Launch two EC2 instances with an Ubuntu AMI and use User Data to install the Apache Web Server
  • Modify the index.html file to include your name and a message
  • Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console
  • Add EC2 instances to the ALB as target groups
  • Verify that the ALB is working properly by checking the health status of the target instances and testing the load balancing capabilities

Let’s get started!

What is Load Balancing?

Load balancing is the distribution of workloads across multiple servers to ensure consistent and optimal resource utilization. It is an essential aspect of any large-scale and scalable computing system, as it helps you to improve the reliability and performance of your applications.

Load balancing can help you to achieve the following benefits:

  • High availability: Load balancing can prevent your applications from becoming unavailable due to server failures or overloading. By distributing the traffic across multiple servers, load balancing can ensure that your applications can handle the requests even if one or more servers go down or become slow.
  • Fault tolerance: Load balancing can also help you to recover from server failures or errors. By detecting the unhealthy or unresponsive servers, load balancing can automatically redirect the traffic to the healthy or available servers, minimizing the impact on your applications and users.
  • Scalability: Load balancing can also help you to scale your applications according to the demand or traffic. By adding or removing servers from the load balancing pool, you can adjust the capacity and performance of your applications without affecting the user experience.

What is Elastic Load Balancing?

Elastic Load Balancing (ELB) is a service provided by Amazon Web Services (AWS) that automatically distributes incoming traffic across multiple EC2 instances. ELB provides three types of load balancers:

  • Application Load Balancer (ALB): ALB operates at layer 7 of the OSI model and is ideal for applications that require advanced routing and microservices. ALB can route the traffic based on the content of the request, such as the URL path, the host header, the query parameters, etc. ALB can also support multiple protocols, such as HTTP, HTTPS, HTTP/2, WebSocket, and gRPC. ALB can also integrate with other AWS services, such as Amazon ECS, Amazon EKS, AWS Lambda, AWS WAF, etc.
  • Network Load Balancer (NLB): NLB operates at layer 4 of the OSI model and is ideal for applications that require high throughput and low latency. NLB can route the traffic based on the TCP or UDP port and the IP address of the destination. NLB can also support static or elastic IP addresses, long-lived TCP connections, and TLS termination. NLB can also integrate with other AWS services, such as Amazon VPC, Amazon EC2, AWS CloudFormation, etc.
  • Classic Load Balancer (CLB): CLB operates at layer 4 of the OSI model and is ideal for applications that require basic load balancing features. CLB can route the traffic based on the TCP or SSL port and the IP address of the destination. CLB can also support HTTP, HTTPS, SSL, and TCP protocols, health checks, sticky sessions, and cross-zone load balancing. CLB can also integrate with other AWS services, such as Amazon EC2, Amazon CloudWatch, etc.

To illustrate how to use ELB in AWS EC2, we’ll use an example of setting up an ALB with two EC2 instances that run the Apache Web Server.

How to Launch Two EC2 Instances with an Ubuntu AMI and Use User Data to Install the Apache Web Server

To launch two EC2 instances with an Ubuntu AMI and use User Data to install the Apache Web Server, we’ll use the following steps:

1. Go to the AWS console and navigate to the EC2 service.

2. Click on Launch Instance button and select Ubuntu Server 22.04 LTS (HVM), SSD Volume Type from the list of available AMIs.

3. Choose t2.micro from the list of available instance types.

4. Choose an existing key pair or create a new one if you don’t have one already. You’ll need this key pair to SSH into your instance later.

5. In the Configure Instance Details page, scroll down to the Advanced Details section and expand the User data field.

6. In the User data field, enter the following commands to install and start the Apache Web Server:

#!/bin/bash
# Update packages
sudo apt-get update -y
# Install Apache
sudo apt-get install -y apache2
# Start Apache service
sudo systemctl start apache2
# Enable Apache service
sudo systemctl enable apache2
# Modify index.html file
sudo echo "<h1>Welcome to AWS EC2</h1>" > /var/www/html/index.html
sudo echo "<p>My name is Ajit</p>" >> /var/www/html/index.html

7. Expand Network settings: Create a new security group or select an existing one. Make sure to allow inbound traffic on port 80 for HTTP protocol from anywhere. This will allow us to access the Apache web page from our browser.

8. Click on Launch instance button and wait for your instance to launch.

9. Repeat steps 2–8 to launch another instance with the same configuration, but modify the user data script as follows:

#!/bin/bash
# Update packages
sudo apt-get update -y
# Install Apache
sudo apt-get install -y apache2
# Start Apache service
sudo systemctl start apache2
# Enable Apache service
sudo systemctl enable apache2
# Modify index.html file
sudo echo "<h1>Welcome to AWS EC2</h1>" > /var/www/html/index.html
sudo echo "<p>TrainWithShubham Community is Super Awesome :)</p>" >> /var/www/html/index.html

This will change the message in the index.html file for the second instance.

You have successfully launched two EC2 instances with an Ubuntu AMI and used User Data to install the Apache Web Server.

How to Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console and Add the EC2 Instances to the ALB as Target Groups

An Application Load Balancer (ALB) is a type of load balancer that operates at layer 7 of the OSI model and is ideal for applications that require advanced routing and microservices. ALB can route the traffic based on the content of the request, such as the URL path, the host header, the query parameters, etc. ALB can also support multiple protocols, such as HTTP, HTTPS, HTTP/2, WebSocket, and gRPC. ALB can also integrate with other AWS services, such as Amazon ECS, Amazon EKS, AWS Lambda, AWS WAF, etc.

To create an ALB in EC2 using the AWS Management Console and add the EC2 instances to the ALB as target groups, we’ll use the following steps:

1.Go to the AWS console and navigate to the EC2 service.

2. Click on Load Balancers in the left navigation pane and then click the Create Load Balancer button.

3. Select Create Application Load Balancer as the load balancer type.

4. In the Basic Configuration section, enter a name for your load balancer, such as ALB-Demo.

5. In the Network mapping section, review VPC and subnet.

6. In the Security Groups section, create a new security group or select an existing one for your load balancer. Make sure to allow inbound traffic on port 80 for HTTP protocol from anywhere. This will allow us to access the load balancer from our browser.

7. In the Listeners and Routing section, create a new target group or select an existing one for your load balancer. A target group is a group of targets, such as EC2 instances, that receive the traffic from the load balancer. For example, you can create a new target group with the following settings:

  • For Name, enter a name for your target group, such as ALB-Demo-TG.
  • For Target type, select the instance to specify targets by instance ID.
  • For Protocol, select HTTP.
  • For Port, enter 80.
  • For Health checks, modify the default settings as needed. For example, you can change the Path to /index.html to check the index.html file on each target.

8. Click on the Create load balancer button and wait for your load balancer to be created.

You have successfully created an ALB in EC2 using the AWS Management Console and added the EC2 instances to the ALB as target groups.

How to Verify that the ALB is Working Properly by Checking the Health Status of the Target Instances and Testing the Load Balancing Capabilities

To verify that the ALB is working properly, we’ll use the following steps:

1.Go to the AWS console and navigate to the EC2 service.

2. Click on Load Balancers in the left navigation pane and select your load balancer, such as ALB-Demo.

3. Copy the DNS name of your load balancer from the details panel. This is the address that you can use to access your load balancer from your browser.

4. Open a web browser and paste the DNS name into the address bar. You should see a webpage displaying information about your apache2 installation and your name or message, such as “Welcome to AWS EC2” and “My name is Ajit”.

5. Refresh the web page several times and observe the changes. You should see different names or messages on each refresh, such as “Welcome to AWS EC2” and “TrainWithShubham Community is Super Awesome :)”. This means that the load balancer is distributing the traffic across your two EC2 instances and displaying the index.html file from each instance.

6. Go back to the AWS console and navigate to the EC2 service.

7. Click on Target Groups in the left navigation pane and select your target group, such as ALB-Demo-TG.

8. Click on the Targets tab and observe the health status of your target instances. You should see that both instances are healthy and in service. This means that the load balancer is able to communicate with your instances and perform health checks on them.

9. To test the fault tolerance of your load balancer, you can stop or terminate one of your instances and see how the load balancer handles the situation. For example, you can select one of your instances, such as ALB-Demo-1, and click on Actions button and choose Instance State and then Stop or Terminate option from the drop-down menu.

10. Wait for the instance to stop or terminate and then refresh the web page in your browser. You should see that the web page is still accessible and displaying the name or message from the other instance, such as “Welcome to AWS EC2” and “TrainWithShubham Community is Super Awesome :)”. This means that the load balancer is able to detect the unhealthy or unavailable instance and redirect the traffic to the healthy or available instance.

You have successfully verified that the ALB is working properly by checking the health status of the target instances and testing the load balancing capabilities.

Conclusion

In this blog post, I have explained what load balancing is and how to use Elastic Load Balancing (ELB) in AWS EC2, a service that automatically distributes incoming traffic across multiple EC2 instances. I have also shown you how to create and use an Application Load Balancer (ALB) in EC2 using the AWS Management Console and add the EC2 instances to the ALB as target groups. I have also shown you how to verify that the ALB is working properly by checking the health status of the target instances and testing the load balancing capabilities.

I hope you found this blog post informative and engaging.

This is the end of the blog post for Day 41 of 90 Days of DevOps.

Thank you for reading and stay tuned for more posts in this series!

--

--

No responses yet