Elastic Load Balancingggggg

π οΈ Step-by-Step: Create and Use Application Load Balancer
πΉ Step 1: Launch EC2 Instances
Launch 2 EC2 instances (Amazon Linux 2):
AMI: Amazon Linux 2
Instance Type: t2.micro
User Data (to install Apache):
bashCopyEdit#!/bin/bash
yum update -y
yum install -y httpd
echo "Hello from EC2-1" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
For 2nd instance, change content to: "Hello from EC2-2"
Security Group (for EC2):
- Inbound Rule: HTTP (80) from
0.0.0.0/0
- Inbound Rule: HTTP (80) from
πΉ Step 2: Create a Target Group
Open EC2 Console β Target Groups β Create Target Group
Target Type:
Instance
Protocol: HTTP, Port: 80
VPC: Select the one where EC2 is running
Name:
web-tg
Health Check Path:
/index.html
Register both EC2s
Click Create
π Now your target group is ready and will monitor instance health.
πΉ Step 3: Create the Application Load Balancer (ALB)
EC2 Console β Load Balancers β Create Load Balancer β Choose Application Load Balancer
Name:
my-alb
Scheme: Internet-facing
IP type: IPv4
Listeners:
Protocol: HTTP
Port: 80
Availability Zones:
- Choose at least 2 subnets in different AZs where your EC2s exist
Security Group for ALB:
- Allow HTTP (80) from
0.0.0.0/0
- Allow HTTP (80) from
Routing:
Target group:
web-tg
Listener rule: forward all requests to this group
Click Create Load Balancer
πΉ Step 4: Test Your ALB
Go to EC2 β Load Balancers β Select
my-alb
Copy DNS Name (e.g.,
my-alb-123456789.ap-south-1.elb.amazonaws.com
)Open in browser β refresh multiple times
β Youβll see:
βHello from EC2-1β
βHello from EC2-2β
(as traffic alternates between targets)
βοΈ How ALB Works (Internally)
User makes HTTP request to ALB DNS.
ALB checks Listener Rules (port 80, HTTP).
Routes request to associated Target Group.
Health check ensures traffic is only sent to healthy EC2s.
Target EC2 receives traffic, processes it, and responds.
ALB forwards the response back to the client.
π§ Optimization Tips for ALB
β 1. Enable HTTP β HTTPS Redirection
Add HTTPS listener (port 443).
Create a certificate using AWS ACM.
Redirect HTTP to HTTPS in listener rules.
β 2. Path-Based Routing (Microservices)
Use multiple Target Groups:
/api/*
β backend EC2 or ECS service/web/*
β frontend EC2
Helps isolate services and scale independently
β 3. Enable Access Logs
Store logs in an S3 bucket
Helps in debugging and analyzing traffic
β 4. Enable Sticky Sessions (Session Affinity)
Go to Target Group β Attributes
Enable stickiness using cookies (useful for stateful apps)
β 5. Auto Scaling Integration
Attach Target Group to an Auto Scaling Group
Automatically adds/removes EC2 instances based on traffic load
β 6. CloudWatch Alarms
Monitor:
HealthyHostCount
TargetResponseTime
HTTPCode_ELB_5XX_Count
Set alarms and notifications for failures.
β 7. Use WAF with ALB
Protect from common threats (SQLi, XSS)
Create a Web ACL and associate it with your ALB
π Recap Summary
Component | Purpose |
EC2 | Web servers serving app |
Target Group | Group of EC2s to receive traffic |
ALB | Distributes HTTP/HTTPS traffic |
Health Check | Ensures only healthy EC2s receive requests |
Listener Rule | Matches request and routes to target group |
β> In next part we will setup a ALB
Subscribe to my newsletter
Read articles from Poonam Vetal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Poonam Vetal
Poonam Vetal
I am student from Pune institute of computer technology !π