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

Urvish SuhagiyaUrvish Suhagiya
4 min read

Understanding Load Balancing

What is Load Balancing?

Load balancing is the process of distributing network traffic across multiple servers to ensure no single server becomes overwhelmed, leading to optimal resource utilization, increased reliability, and improved performance of applications. By spreading the workload evenly, load balancing helps in maintaining the system's availability and responsiveness.

There are different types of load balancing techniques:

  1. Round Robin: Distributes client requests sequentially among available servers.

  2. Least Connections: Directs traffic to the server with the fewest active connections.

  3. IP Hash: Routes requests based on the client’s IP address, ensuring consistent routing to the same server.

Elastic Load Balancing (ELB)

Elastic Load Balancing:

Elastic Load Balancing (ELB) is a service provided by Amazon Web Services (AWS) that automatically distributes incoming application traffic across multiple targets, such as EC2 instances, containers, and IP addresses. ELB offers three types of load balancers, each catering to different use cases:

  1. Application Load Balancer (ALB):

    • Operates at Layer 7 (Application Layer) of the OSI model.

    • Ideal for advanced routing and microservices.

    • Supports path-based routing, host-based routing, and SSL termination.

  2. Network Load Balancer (NLB):

    • Operates at Layer 4 (Transport Layer) of the OSI model.

    • Designed for applications that require high throughput and low latency.

    • Capable of handling millions of requests per second while maintaining ultra-low latencies.

  3. Classic Load Balancer (CLB):

    • Operates at both Layer 4 and Layer 7.

    • Suitable for applications built within the EC2-Classic network.

    • Provides basic load balancing features.

ELB automatically scales its capacity to meet the incoming traffic demands and provides the flexibility to add or remove instances from the load balancer as needed. It also performs health checks on registered targets to ensure that traffic is only directed to healthy instances.


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

Step 1: Launch EC2 Instances

  1. Log in to AWS Management Console

    • Go to the EC2 Dashboard.
  2. Launch EC2 Instances

    • Click on "Launch Instance."

    • Choose the Ubuntu AMI.

    • Select an instance type (e.g., t2.micro for free tier).

    • Configure instance details and add the following User Data script to install Apache Web Server:

        #!/bin/bash
        sudo apt update
        sudo apt install -y apache2
        echo "<h1>Your Name</h1>" | sudo tee /var/www/html/index.html
      
    • Add storage (default settings are usually fine).

    • Add tags (optional).

    • Configure security group to allow HTTP traffic (port 80).

    • Review and launch the instance.

    • Repeat the process for the second instance, but modify the User Data script to include:

        #!/bin/bash
        sudo apt update
        sudo apt install -y apache2
        echo "<h1>TrainWithShubham Community is Super Awesome :)</h1>" | sudo tee /var/www/html/index.html
      
  3. Copy Public IP Addresses

    • After instances are running, copy their public IP addresses.

    • Open a web browser and paste the IP addresses to verify the Apache server is hosting the customized page.

Step 2: Create an Application Load Balancer

  1. Navigate to the EC2 Dashboard

    • Select "Load Balancers" from the left-hand menu.

    • Click on "Create Load Balancer."

  2. Configure Load Balancer

    • Select "Application Load Balancer."

    • Provide a name for the ALB.

    • Choose the scheme (internet-facing).

    • Select appropriate IP address type (IPv4).

    • Configure the listeners (HTTP).

  3. Configure Security Groups

    • Select an existing security group or create a new one that allows HTTP traffic.
  4. Configure Routing

    • Create a new target group for the instances.

    • Choose "Instances" as the target type.

    • Provide a name for the target group.

    • Configure health checks (HTTP, path: /).

  5. Register Targets

    • Add the previously launched EC2 instances to the target group.

    • Click on "Include as pending below" and then "Create Target Group."

  6. Review and Create

    • Review the configurations.

    • Click on "Create Load Balancer."

Step 3: Verify Load Balancer

  1. Check Target Health

    • Navigate to the target group and verify the health status of the instances.

    • Instances should show as "healthy."

  2. Test Load Balancer

    • Copy the DNS name of the ALB.

    • Open a web browser and paste the DNS name.

    • Refresh the page multiple times to see the load balancing in action, displaying content from both instances.

2
Subscribe to my newsletter

Read articles from Urvish Suhagiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Urvish Suhagiya
Urvish Suhagiya

Exploring the world of DevOps 🌐.