Elastic Load Balancingggggg

Poonam VetalPoonam Vetal
3 min read

πŸ› οΈ Step-by-Step: Create and Use Application Load Balancer


πŸ”Ή Step 1: Launch EC2 Instances

Launch 2 EC2 instances (Amazon Linux 2):

  1. AMI: Amazon Linux 2

  2. Instance Type: t2.micro

  3. 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"

  1. Security Group (for EC2):

    • Inbound Rule: HTTP (80) from 0.0.0.0/0

πŸ”Ή Step 2: Create a Target Group

  1. Open EC2 Console β†’ Target Groups β†’ Create Target Group

  2. Target Type: Instance

  3. Protocol: HTTP, Port: 80

  4. VPC: Select the one where EC2 is running

  5. Name: web-tg

  6. Health Check Path: /index.html

  7. Register both EC2s

  8. Click Create

πŸ“Œ Now your target group is ready and will monitor instance health.


πŸ”Ή Step 3: Create the Application Load Balancer (ALB)

  1. EC2 Console β†’ Load Balancers β†’ Create Load Balancer β†’ Choose Application Load Balancer

  2. Name: my-alb

  3. Scheme: Internet-facing

  4. IP type: IPv4

  5. Listeners:

    • Protocol: HTTP

    • Port: 80

  6. Availability Zones:

    • Choose at least 2 subnets in different AZs where your EC2s exist
  7. Security Group for ALB:

    • Allow HTTP (80) from 0.0.0.0/0
  8. Routing:

    • Target group: web-tg

    • Listener rule: forward all requests to this group

  9. Click Create Load Balancer


πŸ”Ή Step 4: Test Your ALB

  1. Go to EC2 β†’ Load Balancers β†’ Select my-alb

  2. Copy DNS Name (e.g., my-alb-123456789.ap-south-1.elb.amazonaws.com)

  3. 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)

  1. User makes HTTP request to ALB DNS.

  2. ALB checks Listener Rules (port 80, HTTP).

  3. Routes request to associated Target Group.

  4. Health check ensures traffic is only sent to healthy EC2s.

  5. Target EC2 receives traffic, processes it, and responds.

  6. 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

ComponentPurpose
EC2Web servers serving app
Target GroupGroup of EC2s to receive traffic
ALBDistributes HTTP/HTTPS traffic
Health CheckEnsures only healthy EC2s receive requests
Listener RuleMatches request and routes to target group

β€”> In next part we will setup a ALB

0
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 !πŸŽ“