☁Setting up an Application Load Balancer with AWS EC2.

Apurva GargoteApurva Gargote
3 min read

πŸ”Ή What is Load Balancing? πŸ€”

Load balancing is the process of distributing incoming traffic across multiple servers to:

  • Prevent overloading any single server

  • Improve application performance

  • Increase fault tolerance

AWS Elastic Load Balancing (ELB)

AWS provides Elastic Load Balancing (ELB) to automatically distribute traffic across multiple EC2 instances. There are three types:

  • Application Load Balancer (ALB) - Operates at Layer 7 (Application Layer) and is ideal for advanced routing and microservices.

  • Network Load Balancer (NLB) - Operates at Layer 4 (Transport Layer) and is optimized for high throughput and low latency.

  • Classic Load Balancer (CLB) - Operates at Layer 4, used for basic load balancing needs.

Today, we will set up an Application Load Balancer (ALB)! πŸš€


πŸ—οΈ How Does ALB Work?

An Application Load Balancer (ALB) consists of:

  • Listeners: Monitor incoming requests and apply routing rules.

  • Target Groups: Group EC2 instances or AWS services for traffic distribution.

  • Health Checks: Continuously monitor the availability of target instances.

  • Rules & Conditions: Define how requests should be routed (e.g., based on path, host, or headers).



🎯 Today's Tasks:

Task 1: Launch EC2 Instances & Install Apache πŸ–₯️

  1. Launch two EC2 instances using an Ubuntu AMI.

  2. Use User Data to install the Apache Web Server on both instances:

     #!/bin/bash
     apt update -y
     apt install apache2 -y
     echo "<h1>YourName's Web Server</h1>" > /var/www/html/index.html
     systemctl restart apache2
    
  3. Modify the index.html file for the second instance:

     echo "<h1>TrainWithShubham Community is Super Awesome :)</h1>" > /var/www/html/index.html
    
  4. Copy the public IP addresses of both instances.

  5. Open a web browser and visit http://<instance_public_ip>.

  6. You should see the respective webpages! πŸŽ‰


Task 2: Set Up an Application Load Balancer (ALB) πŸ—οΈ

  1. Navigate to AWS EC2 Console and select Load Balancers.

  2. Click Create Load Balancer β†’ Select Application Load Balancer.

  3. Configure ALB:

    • Name: My-ALB

    • Scheme: Internet-facing

    • Listener: HTTP (Port 80)

    • VPC: Select your default or existing VPC

    • Availability Zones: Select at least two AZs

  4. Create a Target Group:

    • Name: My-Target-Group

    • Target Type: Instance

    • Protocol: HTTP

    • Port: 80

  5. Register Targets:

    • Select the two EC2 instances from Task 1

    • Click Include as pending below β†’ Register Targets

  6. Review & Create the ALB.


βœ… Testing ALB

  • Open EC2 β†’ Load Balancers β†’ Copy the DNS Name of your ALB.

  • Paste the DNS in a web browser (http://<ALB_DNS>).

  • Refresh multiple times to see requests being routed between the two EC2 instances!


πŸŽ‰ Conclusion

Congratulations! πŸ₯³ You've successfully:

βœ”οΈ Launched two EC2 instances with Apache

βœ”οΈ Created an Application Load Balancer

βœ”οΈ Distributed traffic between instances

This setup ensures scalability, fault tolerance, and high availability for your application. Keep exploring AWS, and happy cloud computing! πŸš€


1
Subscribe to my newsletter

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

Written by

Apurva Gargote
Apurva Gargote

πŸ‘¨β€πŸ’» Last-year student diving deep into DevOps, Cloud Engineering, and Infrastructure Automation. Passionate about building scalable, efficient, and secure systems. Let’s connect and build something amazing! πŸš€