Overview image of AWS Load Balancer tutorial setup steps
Visual summary of AWS Load Balancer setup using EC2 instances and NGINX.

1. Introduction to Load Balancers

Let’s understand what Load balancers are & how it is helpful. Load balancers are used to handle the incoming traffic and distribute it to multiple servers, it prevent server going to a bottleneck, let’s. assume i have one server it has hundred of request at a same time and thus it will not be able to respond to further request in this case if we have another server to handle those traffic it will be a quick response to the end user so that is the reason for using load balancer through which we can navigate traffic to different servers. They help improve the fault tolerance and performance of applications.

Types of Load Balancers in AWS Services:

  • Application Load Balancer (ALB): Best for HTTP/HTTPS traffic, layer 7.
  • Network Load Balancer (NLB): Ideal for ultra-high performance, layer 4.
  • Getway Load Balancer (GWLB): Ultra high-performance, uses GENEVE(Generic Network Virtualization Encapsulation) Protocol on port 6081, layer 3.

In this tutorial, we’ll use the Application Load Balancer, which is perfect for distributing web traffic across multiple EC2 instances.


2. Launching Two EC2 Instances with NGINX Server

To setup a load balance, we require two EC2 instances running Amazon Linux. These will serve as our backend web servers.

Steps:

  1. Go to the EC2 Dashboard in AWS Console.
  2. Click Launch Instance.
  3. Select Amazon Linux AMI.
  4. Choose t2.micro (free tier eligible).
  5. In the Configure Instance section, make sure to launch two instances (set count to 2).
  6. Scroll to Advanced Details and in the User data, add a script to:
    • Install NGINX.
    • Create a basic index.html that shows the private IP of the instance.
#!/bin/bash
sudo apt update
sudo dnf install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx

set +H
echo "<!DOCTYPE html><html><head><title>Welcome to Protocoderspoint</title><style>body {font-family: Arial, sans-serif; background-color: #f0f0f0; text-align: center; padding-top: 50px;} h1 {color: #333;} p, a {color: #444; font-size: 18px;} .button {display: inline-block; margin-top: 20px; padding: 10px 20px; background-color: #FF0000; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;}</style></head><body><h1>Hello from $(hostname -f)</h1><p>My Website: <a href=\"https://protocoderspoint.com\" target=\"_blank\">protocoderspoint.com</a></p><p>Visit and Subscribe to my YouTube Channel:</p><a class=\"button\" href=\"https://www.youtube.com/@protocoderspoint\" target=\"_blank\">Subscribe on YouTube</a></body></html>" > /usr/share/nginx/html/index.html


The Above Script will install nginx and create a html page, On the page will be just print the Private IP Address of the EC2 server, This helps identify which instance serves the request when accessed through the Load Balancer.

User Data script to install NGINX and display EC2 private IP
User Data setup to auto-install NGINX and show EC2 instance private IP on webpage.
AWS EC2 dashboard showing two running instances
Two Amazon EC2 instances are up and running for load balancing setup.

3. Configuring Security Group with Port 80 Access

To access your NGINX server, you need to open port 80 in the instance’s Security Group.

Steps:

  1. In the Configure Security Group step of EC2 launch:
    • Add HTTP (port 80) under Inbound Rules.
    • You may also allow SSH (port 22) for troubleshooting (optional).
  2. Name your security group for easy identification.
EC2 Security Group with inbound rule allowing HTTP traffic on port 80
Security Group configured to allow inbound HTTP traffic on port 80 for EC2 access.

4. Verifying EC2 Instance Web Servers

Once your EC2 instances are running:

  1. Navigate to the Instances section.
  2. Copy the Public IP of each instance.
  3. Open a browser and visit http://<EC2_Public_IP>.

You should see the web page served by NGINX, displaying the Private IP of that instance.

EC2 instance responding with HTML page showing private IP
EC2 instance successfully serves the custom HTML page via NGINX.

Repeat the same and check the second instance to confirm both are working.


5. Creating a Load Balancer

Now that both servers are ready, let’s create a Load Balancer to distribute traffic among the above 2 instances been created.

Steps:

  1. Go to the EC2 DashboardLoad Balancers.
  2. Click Create Load Balancer.
  3. Choose Application Load Balancer.
  4. Provide a name (e.g., myLB).
  5. Set the scheme to Internet-facing.
  6. Choose IPv4 and select at least two Availability Zones with public subnets.
Selecting Application Load Balancer option in AWS
Click on “Application Load Balancer” to begin creating a new load balancer.
Selecting all Availability Zones during Load Balancer setup
Select all Availability Zones to ensure high availability across multiple regions.

6. Setting Up Security Group on Port 80

You’ll need to assign a security group to the Load Balancer that allows HTTP traffic.

Steps:

  1. Create or choose a Security Group with Inbound Rule on port 80 (HTTP).
  2. This security group will be associated with the Load Balancer, not EC2.
Security Group configuration for AWS Load Balancer allowing HTTP traffic
Security Group for Load Balancer configured to allow inbound HTTP traffic on port 80.

7. Creating Target Group and Registering EC2 Instances

Now we need to create a Target Group that defines how the Load Balancer routes requests to the registered EC2 instances, depending on the traffic.

Steps:

  1. In Load Balancer setup, create a new Target Group:
    • Target type: Instance
    • Protocol: HTTP
    • Port: 80
  2. After creation, go to the Targets tab and click Register Targets.
  3. Add both EC2 instances and confirm.
Creating a target group to register EC2 instances in AWS
Click on “Create Target Group” to start adding EC2 instances for load balancing.
Target Group created successfully in AWS console
Target Group created and ready for registering EC2 instances.

8. Completing Load Balancer Setup

Once the Target Group is set, finish creating the Load Balancer:

  1. Attach the listener to HTTP (port 80).
  2. Select your previously created Target Group.
  3. Click Create Load Balancer.

AWS will take a couple of minutes to provision and bring the Load Balancer into service.

AWS Load Balancer created successfully and showing active status
Load Balancer created successfully and is now in active state.

9. Testing with Load Balancer DNS URL

After the Load Balancer is active:

  1. Go to the Description tab of your Load Balancer.
  2. Copy the DNS Name.
  3. Visit the DNS in your browser (http://<LoadBalancerDNS>).

Refresh the page multiple times. You should see the Private IP change as the Load Balancer alternates requests between the two EC2 instances.

Load Balance Responce from first EC2 Instance

Load balancer routing response from the first EC2 instance displaying its private IP in the browser.
Response from Load Balancer directed to EC2 Instance 1 showing its private IP.
Load balancer routing response from the second EC2 instance displaying its private IP in the browser.
Response from Load Balancer directed to EC2 Instance 2 showing its private IP.

Conclusion

Hurry Congratulations! Now we have successfully created a Load Balancer in AWS with two EC2 instances that simply serve a HTML page, then print the IP address of the respective server. This load-balancing setup ensures that your web traffic is evenly distributed, offering both performance and fault tolerance.