Load Balancing in AWS
1. Layer 4 Load Balancing
AWS provides a Network Load Balancer (NLB) which operates at Layer 4 of the OSI model.
What Layer 4 understands:
| Attribute | Detail |
|---|---|
| IP | Source/Destination IP address |
| Protocol | TCP / UDP |
| Ports | Port numbers (e.g., 80, 443) |
Layer 4 does not inspect HTTP headers, paths, or cookies — that’s Layer 7 (ALB).
Key Characteristics:
- AWS load balancers can be internal (private) or external (public)
- AWS supports Auto Scaling Groups — instances can be added/removed dynamically based on load
- Load balancers forward requests only to healthy EC2 instances using health checks
2. Core Concepts
Target Group
- A logical grouping of EC2 instances that receive traffic from the load balancer
- Instances can be scattered across multiple subnets within a VPC
- Health checks are configured at the target group level
Load Balancer Components
| Component | Description |
|---|---|
| Target Group | Set of backend EC2 instances |
| Listener | Rules that define how traffic is routed (protocol + port) |
Health Checks
- NLB periodically checks each target in the target group
- Only healthy instances receive traffic
- Unhealthy instances are automatically taken out of rotation
3. Lab: End-to-End NLB Setup
Architecture Overview
Internet
│
▼
[Network Load Balancer] ← external, public
│ │
▼ ▼
[EC2 - AZ-1] [EC2 - AZ-2] ← no public IP
(Subnet-1) (Subnet-2)
└─────┬─────┘
│
[VPC]
(2 public subnets)
Step 1: Create a Custom AMI with Nginx Website
Launch a base Ubuntu EC2 instance and run:
sudo apt update
sudo apt install nginx unzip -y
