βSetting up an Application Load Balancer with AWS EC2.


πΉ 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 π₯οΈ
Launch two EC2 instances using an Ubuntu AMI.
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
Modify the index.html file for the second instance:
echo "<h1>TrainWithShubham Community is Super Awesome :)</h1>" > /var/www/html/index.html
Copy the public IP addresses of both instances.
Open a web browser and visit
http://<instance_public_ip>
.You should see the respective webpages! π
Task 2: Set Up an Application Load Balancer (ALB) ποΈ
Navigate to AWS EC2 Console and select Load Balancers.
Click Create Load Balancer β Select Application Load Balancer.
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
Create a Target Group:
Name:
My-Target-Group
Target Type: Instance
Protocol: HTTP
Port: 80
Register Targets:
Select the two EC2 instances from Task 1
Click Include as pending below β Register Targets
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! π
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! π