Elastic Load Balancing Explained: Application, Network and Gateway Load Balancers, Target Groups and Health Checks
Why a Load Balancer?
- One server is a single point of failure and has limited power
- A load balancer sits in front of several servers, spreads requests among them and stops sending traffic to broken servers
- Users see one address while you can add or remove servers behind it
text
Users ──► Load balancer (DNS name) ──► Server in AZ a
├─► Server in AZ b
└─► Server in AZ c (unhealthy servers are skipped)
Types of Load Balancers
| Type | Works at | Best for | Notes |
|---|---|---|---|
| Application Load Balancer (ALB) | Layer 7 (HTTP/HTTPS) | Websites, APIs, microservices, containers | Routes by path, host name, headers. Supports WebSockets, redirects, authentication, WAF, Lambda targets |
| Network Load Balancer (NLB) | Layer 4 (TCP/UDP/TLS) | Extreme performance, low latency, static IP per AZ, non-HTTP protocols | Millions of requests per second |
| Gateway Load Balancer (GWLB) | Layer 3 | Firewalls and inspection appliances from partners | Transparent traffic inspection |
| Classic Load Balancer | Layer 4/7 | Old applications only | Do not use for new work |
Key Pieces of an ALB
| Piece | Meaning |
|---|---|
| Listener | Checks for connections on a port and protocol (for example HTTP 80, HTTPS 443) |
| Rules | Decide what to do with a request: forward, redirect, return a fixed response. Match by path (/api/*), host (shop.example.com), header |
| Target group | A group of targets (EC2 instances, IP addresses, Lambda functions, containers) that receive traffic |
| Health check | Regular test of each target, for example GET /health expecting HTTP 200 |
| Subnets / AZs | The balancer needs subnets in at least two AZs |
| Security group | Controls who can reach the balancer. Servers should allow traffic only from the balancer's security group |
Create an ALB Step by Step
- Launch two EC2 web servers in two AZs (use the nginx user data)
- Create a target group (instances, HTTP 80, health check path
/) and register both servers - Create an Application Load Balancer (internet-facing) in two public subnets with a security group allowing 80/443 from anywhere
- Add a listener on port 80 that forwards to the target group
- Wait until targets are healthy
- Open the ALB DNS name in a browser and refresh to see both servers answer
bash
TG=$(aws elbv2 create-target-group --name web-tg --protocol HTTP --port 80 --vpc-id vpc-0abc --health-check-path /health --query 'TargetGroups[0].TargetGroupArn' --output text)
aws elbv2 register-targets --target-group-arn $TG --targets Id=i-0aaa Id=i-0bbb
ALB=$(aws elbv2 create-load-balancer --name web-alb --subnets subnet-a subnet-b --security-groups sg-alb --query 'LoadBalancers[0].LoadBalancerArn' --output text)
aws elbv2 create-listener --load-balancer-arn $ALB --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=$TG
aws elbv2 describe-target-health --target-group-arn $TG --query 'TargetHealthDescriptions[].[Target.Id,TargetHealth.State]' --output table
HTTPS and Security
- Request a free certificate in AWS Certificate Manager (ACM) and add an HTTPS listener on 443
- Redirect HTTP 80 to HTTPS 443 with a listener rule
- Choose a modern security policy for TLS versions
- Attach AWS WAF to an ALB to block common attacks
- Keep servers in private subnets, with only the ALB in public subnets
Useful Settings
| Setting | Meaning |
|---|---|
| Cross-zone load balancing | Spread traffic evenly across all targets in all AZs (always on for ALB) |
| Sticky sessions | Send the same user to the same server using a cookie (better to keep servers stateless) |
| Deregistration delay | Time to finish in-flight requests before removing a target (default 300 s) |
| Idle timeout | How long a connection can stay idle (default 60 s) |
| Access logs | Request logs saved to S3 for troubleshooting |
| Connection draining and slow start | Smooth deployments and scaling |
Troubleshooting
| Error / symptom | Likely cause |
|---|---|
| 502 Bad Gateway | Server crashed or sent an invalid response, or closed the connection |
| 503 Service Unavailable | No healthy targets |
| 504 Gateway Timeout | Server too slow, or blocked by security group or NACL |
| Targets unhealthy | Wrong health check path or port, app not running, server security group does not allow the balancer |
| Cannot open the ALB URL | Security group on ALB, not in public subnets, no route to Internet Gateway |
Practice
- Build the ALB with two servers and refresh the page to see load balancing
- Stop nginx on one server and watch it become unhealthy (and traffic still works)
- Add a path rule
/api/*to a second target group - Add an HTTPS listener with an ACM certificate
- Enable access logs to S3
Conclusion
A load balancer spreads traffic and hides failures. Use ALB for HTTP apps, NLB for TCP/UDP and static IPs, GWLB for inspection appliances. Understand listeners, rules, target groups and health checks, and read 502, 503 and 504 errors.
Quick Summary: why; types; pieces; create; HTTPS; settings; troubleshooting.

