Skip to main content

AWS Load Balancers

What is a Load Balancer?​

A Load Balancer distributes incoming traffic among multiple servers to improve availability and performance.

┌─────────────────────────────────────────────────────────────┐
│ LOAD BALANCER - CONCEPT │
├─────────────────────────────────────────────────────────────┤
│ │
│ Users │
│ (1000 requests) │
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Load Balancer│ │
│ │ │ │
│ │ Distributes │ │
│ │ traffic │ │
│ └──────┬───────┘ │
│ │ │
│ ┌─────────────┼─────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌────────┐ ┌────────┐ ┌────────┐ │
│ │ Server │ │ Server │ │ Server │ │
│ │ 1 │ │ 2 │ │ 3 │ │
│ │ ~333 │ │ ~333 │ │ ~333 │ │
│ │ req │ │ req │ │ req │ │
│ └────────┘ └────────┘ └────────┘ │
│ │
│ Advantages: │
│ ✅ Load distribution │
│ ✅ High availability │
│ ✅ Horizontal scaling │
│ ✅ Automatic health checks │
│ │
└─────────────────────────────────────────────────────────────┘

Types of AWS Load Balancers​

Comparison​

┌─────────────────────────────────────────────────────────────┐
│ TYPES OF AWS LOAD BALANCERS │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ALB (Application) NLB (Network) │ │
│ │ ───────────────── ─────────────── │ │
│ │ Layer 7 (HTTP) Layer 4 (TCP/UDP) │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ HTTP/HTTPS │ │ TCP/UDP │ │ │
│ │ │ │ │ │ │ │
│ │ │ Path-based │ │ Ultra-fast │ │ │
│ │ │ routing │ │ millions │ │ │
│ │ │ │ │ req/sec │ │ │
│ │ └─────────────┘ └─────────────┘ │ │
│ │ │ │
│ │ Use cases: Use cases: │ │
│ │ • Websites • Gaming │ │
│ │ • REST APIs • IoT │ │
│ │ • Microservices • VoIP │ │
│ │ • Pure TCP │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ GWLB (Gateway) CLB (Classic) │ │
│ │ ────────────── ───────────── │ │
│ │ Layer 3 (IP) Legacy │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Appliances │ │ OLD │ │ │
│ │ │ virtual │ │ Avoid │ │ │
│ │ │ │ │ │ │ │
│ │ │ Firewalls │ │ Use │ │ │
│ │ │ IDS/IPS │ │ ALB/NLB │ │ │
│ │ └─────────────┘ └─────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

Comparison Table​

FeatureALBNLBGWLBCLB
OSI Layer7434/7
ProtocolsHTTP, HTTPS, gRPCTCP, UDP, TLSIPTCP, SSL, HTTP
PerformanceGoodUltraVery goodAverage
Static IPNoYesYesNo
WebSocketYesYesNoYes
Advanced RoutingYesNoNoNo
Price$$$$$$$$$
Recommended✅ Web✅ TCPFirewalls❌ Legacy

Application Load Balancer (ALB)​

ALB Architecture​

┌─────────────────────────────────────────────────────────────┐
│ ALB ARCHITECTURE │
├─────────────────────────────────────────────────────────────┤
│ │
│ Internet │
│ │ │
│ ▼ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ ALB │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ LISTENERS │ │ │
│ │ │ │ │ │
│ │ │ ┌────────────┐ ┌────────────┐ │ │ │
│ │ │ │ Port 80 │ │ Port 443 │ │ │ │
│ │ │ │ HTTP │ │ HTTPS │ │ │ │
│ │ │ └─────┬──────┘ └─────┬──────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ └────────┼──────────────────┼─────────────────────┘ │ │
│ │ │ │ │ │
│ │ ┌────────┼──────────────────┼─────────────────────┐ │ │
│ │ │ │ RULES │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ IF path=/api/* IF path=/images/* │ │ │
│ │ │ │ │ │ │ │
│ │ └────────┼──────────────────┼─────────────────────┘ │ │
│ │ │ │ │ │
│ └───────────┼──────────────────┼─────────────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌───────────────────┐ ┌───────────────────┐ │
│ │ TARGET GROUP │ │ TARGET GROUP │ │
│ │ "api-servers" │ │ "image-servers" │ │
│ │ │ │ │ │
│ │ ┌────┐ ┌────┐ │ │ ┌────┐ ┌────┐ │ │
│ │ │EC2 │ │EC2 │ │ │ │EC2 │ │EC2 │ │ │
│ │ └────┘ └────┘ │ │ └────┘ └────┘ │ │
│ └───────────────────┘ └───────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

ALB Components​

ComponentDescriptionExample
ListenerEntry point (port)HTTP:80, HTTPS:443
RuleRouting conditionIF path=/api/* THEN...
Target GroupGroup of target serversEC2, IP, Lambda
TargetIndividual serveri-xxx on port 8080

Advanced ALB Routing​

┌─────────────────────────────────────────────────────────────┐
│ ALB ROUTING RULES │
├─────────────────────────────────────────────────────────────┤
│ │
│ Available conditions: │
│ │
│ • Path: /api/*, /images/*, /static/* │
│ • Host: api.example.com, www.example.com │
│ • Headers: X-Custom-Header = value │
│ • Query: ?version=v2 │
│ • Source IP: 10.0.0.0/8 │
│ • HTTP Method: GET, POST │
│ │
│ Example rules: │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Rule 1: IF path = /api/* │ │
│ │ THEN forward to tg-api-servers │ │
│ │ │ │
│ │ Rule 2: IF host = admin.example.com │ │
│ │ THEN forward to tg-admin │ │
│ │ │ │
│ │ Rule 3: IF query = ?v2 │ │
│ │ THEN forward to tg-api-v2 │ │
│ │ │ │
│ │ Default: forward to tg-web-servers │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

ALB Configuration​

Via AWS CLI​

# 1. Create the Target Group
aws elbv2 create-target-group \
--name tg-web-servers \
--protocol HTTP \
--port 80 \
--vpc-id vpc-xxx \
--target-type instance \
--health-check-path /health \
--health-check-interval-seconds 30

# 2. Create the ALB
aws elbv2 create-load-balancer \
--name my-alb \
--subnets subnet-1a subnet-1b subnet-1c \
--security-groups sg-alb \
--scheme internet-facing \
--type application

# 3. Create the HTTP Listener
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:...:my-alb \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:...:tg-web-servers

# 4. Register the targets
aws elbv2 register-targets \
--target-group-arn arn:aws:...:tg-web-servers \
--targets Id=i-server1 Id=i-server2

Security Group for ALB​

┌─────────────────────────────────────────────────────────────┐
│ SECURITY GROUPS ALB + EC2 │
├─────────────────────────────────────────────────────────────┤
│ │
│ sg-alb (for the Load Balancer) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Inbound: │ │
│ │ HTTP (80) from 0.0.0.0/0 │ │
│ │ HTTPS (443) from 0.0.0.0/0 │ │
│ │ │ │
│ │ Outbound: │ │
│ │ HTTP (80) to sg-web-servers │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ sg-web-servers (for the EC2) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Inbound: │ │
│ │ HTTP (80) from sg-alb ← ONLY from ALB │ │
│ │ SSH (22) from sg-bastion │ │
│ │ │ │
│ │ Outbound: │ │
│ │ All traffic to 0.0.0.0/0 │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ ⚠️ EC2 only accepts HTTP traffic FROM the ALB │
│ → Direct access to EC2 is not possible │
│ │
└─────────────────────────────────────────────────────────────┘

Network Load Balancer (NLB)​

When to use NLB?​

┌─────────────────────────────────────────────────────────────┐
│ WHEN TO USE NLB │
├─────────────────────────────────────────────────────────────┤
│ │
│ ✅ USE NLB FOR: │
│ │
│ • Extreme performance (millions of requests/sec) │
│ • Ultra-low latency (< 100 μs) │
│ • Pure TCP/UDP protocols (not HTTP) │
│ • Need for static IP │
│ • Gaming, IoT, streaming │
│ • Applications not supporting proxy │
│ │
│ ❌ DO NOT USE NLB FOR: │
│ │
│ • Standard websites (use ALB) │
│ • REST APIs (use ALB) │
│ • Need for path/host-based routing │
│ │
│ NLB Features: │
│ • Preserves client's source IP │
│ • Static IP or Elastic IP possible │
│ • Zonal isolation (failover per AZ) │
│ • Cross-zone load balancing (optional) │
│ │
└─────────────────────────────────────────────────────────────┘

Load Balancer Costs​

┌─────────────────────────────────────────────────────────────┐
│ COST ESTIMATION (us-east-1) │
├─────────────────────────────────────────────────────────────┤
│ │
│ ALB: │
│ • $0.0225 / hour ≈ $16 / month (fixed) │
│ • $0.008 / LCU-hour (usage) │
│ • Typical estimate: $20-50 / month │
│ │
│ NLB: │
│ • $0.0225 / hour ≈ $16 / month (fixed) │
│ • $0.006 / NLCU-hour (usage) │
│ • Typical estimate: $20-100 / month │
│ │
│ LCU (Load Balancer Capacity Unit): │
│ Based on: │
│ • New connections / second │
│ • Active connections │
│ • Processed data │
│ │
└─────────────────────────────────────────────────────────────┘

Summary​

AspectALBNLB
Layer7 (HTTP)4 (TCP)
Use casesWeb, APIPure TCP, gaming
Advanced routing✅ Yes❌ No
Static IP❌ No✅ Yes
PerformanceGoodExtreme
Recommendation90% of casesSpecial cases

Next section: Health Checks