Handle EC2 server traffic using Load balancing in AWS.

Rohit Jain
5 min readAug 6, 2023

--

Introduction to the Load balancer

A single server can able to handle the huge traffic, you need to scale your servers either horizontally or vertically. In this tutorial, I will discuss horizontal scaling. In horizontal scaling, we increase the no of instances and balance the load using load balancers. When we create multiple servers then it will create different IP addresses for each server and we need a single endpoint to trigger our server, so the load balancer will become that starting endpoint and send the request to our server based on some rules.

Prerequisite:- AWS account

Different steps to be followed in this blog

  1. Create security groups
  2. Create EC2 instances
  3. Create a target group and attach our ec2 instances
  4. Attach Load balancer

Create Security groups

We have to create 2 Security groups first one for the EC2 instance and the second one for the load balancer. So the reason behind the two security groups is we want the load balancer to be accessed publically and the instance can only be accessed by the load balancer. So we add an HTTP port in the inbound rule of the load balancer security group and in the case of the EC2 instance, we will add the security group of the load balancer in the inbound rules.

Load balancer Security Group
EC2 Instance Security Group

Create EC2 instance:-

In my previous blog, I taught you how to create an Ec2 instance and run Apache server on it using httpd. Just create 2 EC2 instances in the same way given in that blog but there are two changes first one is to use the security group for instance which we have created in the above step and instead of echoing “Hello world”, echo the below text by running the below command.

echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

Create a target group and attach our EC2 instance:-

Target group:- In simple terms, a target group is a group of instances, functions, IP addresses etc. // do it later

Create a target group:-

  1. In the EC2 dashboard select the target group from the sidebar, then select the target type in our case it is “instances”, then enter the target group name ex:- “Load-balancer-target-group” and click on the Next button.

2. In the next step select the EC2 instances you want to register in this target group. In our case select all the 3 instances you have created in the above step and then click on the “Include as pending below” button then click on the “Create target group” button.

Attach Load balancer:-

Now our target group is created our next task is to create an application load balancer(ALB) and attach the target group to this ALB. The Application load balancer works HTTP and HTTPS ports so the users can easily able to access the load balancer using public IP address. Now whenever the user hit the load balancer IP address the load balancer does some calculations and selects an Ec2 instance based on their calculation from the target group and sends the request to that instance.

To create an application load balancer first go to the EC2 dashboard, select load balancer from the sidebar and then choose the ALB option.

After choosing you will redirect to the next page, where you have to enter the name of the load balancer.

Then select the VPC if you have created it or use the default one. Select all the availability zones to make our application available all the time.

Now select the security group of ALB and the target group which we have created in the above steps. Then scroll to the end and click on the Create load balancer button.

Now open the public DNS of the load balancer in any browser and continuously refresh the page, you will get to know that the load balancer sends the request to different EC2 instances. In the below image, you can see that the URL is the same but the EC2 instances are different. It means the load balancer balances the load to different EC2 instances.

Instance-1 IP Address
Instance-2 IP Address

Congratulation, you have learnt how to scale your application horizontally and balance the load using a load balancer.

The next blog will be the continuation of this blog. In the next blog, I will teach you how you can attach an Auto-Scaling Group to our instances so that our instances are automatically created and terminated according to server load.

If you have any queries then connect me on LinkedIn, and I will help you there.

LinkedIn Profile link:- https://www.linkedin.com/in/rohitjain0301/

Thank you.

--

--