Elastic Load Balancing (ELB) spreads incoming traffic across several targets, such as EC2 instances, so no single server is overloaded and one failure does not take the app down. The most common type for web apps is the Application Load Balancer (ALB).
What it does
- Receives traffic on a listener, such as HTTP port 80 or HTTPS port 443.
- Sends each request to a healthy target in a target group.
- Runs health checks and stops sending traffic to unhealthy targets.
- Works across Availability Zones, so the app survives the loss of one.
Key concepts
| Piece | What it is |
|---|---|
| Load balancer | The entry point with a DNS name |
| Listener | Checks for connections on a port and protocol |
| Rule | Decides where a request goes, for example by path or host name |
| Target group | The set of targets that receive traffic |
| Health check | A probe that marks a target healthy or unhealthy |
| Application Load Balancer | Works at layer 7, for HTTP and HTTPS |
| Network Load Balancer | Works at layer 4, for TCP and very high performance |
Good to know
- An ALB needs subnets in at least two Availability Zones.
- Use path-based or host-based rules to send
/apiand/imagesto different target groups. - Let the load balancer's security group accept internet traffic, and let the instances accept traffic only from the load balancer's security group.
- Put HTTPS on the listener with a certificate from AWS Certificate Manager.
- Point your domain at the load balancer with a Route 53 alias record.
- Combine it with Auto Scaling so the number of targets follows demand.
Hands-on: try it in your own account
- Launch two EC2 instances in different subnets and Availability Zones. In the user data, install a web server and write a page that shows the instance name.
- Create a security group for the load balancer that allows HTTP from anywhere.
- Allow HTTP on the instance security group only from the load balancer's security group.
- Open EC2 > Target Groups and create a group for instances with HTTP port 80 and the health check path
/. Add the two instances. - Create an Application Load Balancer that is internet-facing, choose both Availability Zones, and add a listener on port 80 that forwards to the target group.
- Wait until the targets show healthy, then open the load balancer's DNS name and refresh a few times to see both instances answer.
- Stop one instance and watch its health check fail while the page keeps working.
- Clean up: delete the load balancer, the target group, and terminate the instances, because a load balancer has an hourly charge.
Common mistakes
- Choosing subnets in only one Availability Zone.
- Opening the instances to the internet instead of only to the load balancer.
- Using a health check path that returns an error, so every target looks unhealthy.
- Forgetting that the load balancer costs money while it exists.
- Sending HTTPS traffic to a listener that has no certificate.
Key takeaways
- A load balancer spreads traffic and hides failed targets.
- Health checks and multiple Availability Zones give you resilience.
- Keep instances private to the load balancer's security group.
- Delete practice load balancers when you are done.
Tomorrow I'll start a new AWS topic.
Part of #120DaysOfAWS.
Top comments (0)