Elastic Load Balancing
A load balancer spreads incoming traffic across multiple targets so no single instance is overwhelmed.
Elastic Load Balancing (ELB) automatically distributes incoming traffic across multiple targets (EC2 instances, containers, IPs, or Lambda) in one or more Availability Zones. It also runs health checks and stops sending traffic to unhealthy targets.
Types of load balancer
- Application Load Balancer (ALB) — Layer 7 (HTTP/HTTPS). Routes by path, host or header. Best for web apps and microservices.
- Network Load Balancer (NLB) — Layer 4 (TCP/UDP). Extreme performance and static IPs.
- Gateway Load Balancer — for deploying third-party network appliances.
Key pieces
- Listener — the port/protocol the LB listens on.
- Target group — the set of backends plus their health check.
Load balancers pair naturally with Auto Scaling to build resilient, elastic tiers.
Example
# Create an internet-facing Application Load Balancer
aws elbv2 create-load-balancer \
--name web-alb --type application \
--subnets subnet-0a1b2c3d subnet-0e5f6a7b \
--security-groups sg-0123456789abcdef0
# Create a target group and register instances
aws elbv2 create-target-group --name web-targets \
--protocol HTTP --port 80 --vpc-id vpc-0a1b2c3d4e5f67890 \
--health-check-path /healthWhen to use it
- An Application Load Balancer distributes HTTPS traffic across five EC2 instances and routes /api/* requests to a separate target group for the API tier.
- A Network Load Balancer handles TCP traffic for a gaming backend that requires ultra-low latency and millions of connections per second.
- A load balancer performs health checks every 30 seconds and automatically removes unhealthy instances from the pool until they recover.
More examples
Create an Application Load Balancer
Creates an internet-facing Application Load Balancer across two public subnets for high-availability HTTP/HTTPS routing.
aws elbv2 create-load-balancer \
--name web-alb \
--type application \
--subnets subnet-public-1a subnet-public-1b \
--security-groups sg-alb-0abc123 \
--query 'LoadBalancers[0].{ARN:LoadBalancerArn,DNS:DNSName}' \
--output tableRegister EC2 Targets to ALB
Creates a target group with a health check path and registers two EC2 instances as targets for the load balancer.
# Create target group
TG=$(aws elbv2 create-target-group \
--name web-tg \
--protocol HTTP \
--port 80 \
--vpc-id vpc-0abc123 \
--health-check-path /health \
--query 'TargetGroups[0].TargetGroupArn' --output text)
# Register instances
aws elbv2 register-targets \
--target-group-arn $TG \
--targets Id=i-0abc123 Id=i-0def456Add HTTPS Listener to ALB
Attaches an HTTPS listener with an ACM certificate to the ALB so it terminates TLS and forwards traffic to the target group.
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/web-alb/abc \
--protocol HTTPS \
--port 443 \
--certificates CertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/abc123 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/web-tg/abc
Discussion