Skip to Content

Elastic Load Balancing

What a load balancer does, why to use a managed one, health checks, listeners and target groups, the four ELB types, and security groups.

What it is

A load balancer is a server that receives requests and forwards them to several servers behind it (the targets), such as EC2 instances. Elastic Load Balancing (ELB) is AWS's managed load balancer service.

Users reach one load balancer by DNS name; it sends requests to two healthy EC2 instances and none to the instance that failed its health check Users reach one load balancer by DNS name; it sends requests to two healthy EC2 instances and none to the instance that failed its health check

One entry point, several targets.

Why use a load balancer

  • Spread the load across many instances.
  • One point of access, a DNS name, for the whole application, however many instances there are.
  • Handle failures smoothly: traffic goes only to healthy instances.
  • Health checks on every instance.
  • SSL/TLS termination (HTTPS) for websites.
  • Stickiness with cookies, when an app needs it.
  • High availability across AZs.
  • Separate public from private traffic: the load balancer is public, the instances can stay private.

Why a managed (Elastic) load balancer

  • AWS guarantees it works, and handles upgrades, maintenance and its own high availability. It scales itself as traffic grows.
  • AWS offers only a few configuration knobs.
  • Running a self-managed one (for example NGINX or HAProxy on EC2) can cost less, but it's much more work.
  • It's integrated with many services: EC2, Auto Scaling groups, ECS, AWS Certificate Manager (ACM), CloudWatch, Route 53, AWS WAF and Global Accelerator.

How it works

  • A listener checks for connections on a protocol and port, for example HTTPS on 443.
  • The listener's rules send each request to a target group: a set of targets (instances, IP addresses, Lambda functions or containers) with the protocol and port to use.
  • The load balancer runs a node in each enabled AZ, and DNS spreads clients across the nodes. (An Application Load Balancer needs at least two AZs.)
  • A load balancer is internet-facing (public DNS name and IPs) or internal (private IPs, reachable only inside the VPC or connected networks).

Health checks

Health checks let the load balancer know whether a target can answer requests.

  • Set per target group: a protocol, port and path, for example HTTP, port 80, path /health.
  • A target is healthy if it answers with a success code (200 by default for HTTP) for a number of checks in a row, and unhealthy after several failures.
  • Unhealthy targets get no traffic until they pass again. An Auto Scaling group can also use these results to replace them.

The four types

Application LB at layer 7, Network LB at layer 4, Gateway LB at layer 3, and the old Classic LB Application LB at layer 7, Network LB at layer 4, Gateway LB at layer 3, and the old Classic LB

Pick by the layer and protocol needed.
  • Application Load Balancer (ALB, 2016): layer 7, HTTP, HTTPS, WebSocket and gRPC. Routes by URL path, hostname, headers and more.
  • Network Load Balancer (NLB, 2017): layer 4, TCP, UDP, TLS and QUIC. Millions of requests per second, static IPs.
  • Gateway Load Balancer (GWLB, 2020): layer 3, IP packets. Sends traffic through firewalls and other appliances.
  • Classic Load Balancer (CLB, 2009): the old generation, layers 4 and 7 (HTTP, HTTPS, TCP, SSL). One fixed hostname per CLB, one certificate.

Use the newer types: they have more features. AWS now recommends migrating Classic Load Balancers to an ALB or NLB.

Security groups

The load balancer security group allows 443 from anywhere; the instance security group allows 80 only from the load balancer security group The load balancer security group allows 443 from anywhere; the instance security group allows 80 only from the load balancer security group

Users reach the instances only through the load balancer.
  1. The load balancer's security group allows HTTP and HTTPS (80, 443) from anywhere (0.0.0.0/0).
  2. The instances' security group allows HTTP (80) only from the load balancer's security group, by referencing it.
  3. Nobody can reach the instances directly, even if they guess an IP.

Pricing

  • Each load balancer is billed per hour plus capacity units for the traffic it handles (LCUs for ALB, NLCUs for NLB, GLCUs for GWLB). An ALB in us-east-1 costs about $0.0225 an hour (about $16 a month) before capacity units.
  • An internet-facing load balancer also pays for its public IPv4 addresses ($0.005 an hour each, one per AZ).
  • Data transfer is extra, including cross-AZ traffic in some cases (see cross-zone load balancing).

Exam tips

  • "Single DNS name in front of many instances", "distribute traffic", "remove unhealthy instances" → Elastic Load Balancing.
  • "Instances must only accept traffic from the load balancer" → instance security group references the load balancer's security group.
  • "HTTP routing by path or host" → ALB. "TCP/UDP, extreme performance, static IP" → NLB. "Third-party firewall appliances" → GWLB.
  • "Classic Load Balancer" in an answer is almost never right for a new design.

Common traps

  • "A load balancer makes the app highly available on its own." Only if targets run in at least two AZs.
  • "Allow 0.0.0.0/0 on the instances too." Then the load balancer can be bypassed.
  • "A health check failure terminates the instance." The load balancer only stops sending traffic; an Auto Scaling group with ELB health checks replaces it.

Hands-on lab: two web servers behind an ALB

Goal: put two instances behind an Application Load Balancer and watch it spread requests and skip a failed instance.

Costs money

Two t3.micro instances (about $0.02 an hour each with public IPs), plus the ALB at about $0.0225 an hour, two public IPv4 addresses ($0.01 an hour) and a tiny capacity-unit charge: under $0.10 an hour in total. Delete the ALB at the end: it bills by the hour even with no traffic.

Launch two web servers

Launch two t3.micro instances (saa-web-1, saa-web-2), Amazon Linux 2023, tag Project=saa-lab, in two different subnets (AZs), with security group saa-web-sg from the EC2 basics lab and the same web server user data. Each page shows its own hostname.

Create a target group

EC2 → Target groups → Create target group: type Instances, name saa-tg, protocol HTTP, port 80, health check path /. Register both instances on port 80 and create.

Create the load balancer

Load balancers → Create → Application Load Balancer: name saa-alb, internet-facing, IPv4, the default VPC, at least two AZs (the ones your instances are in). Create a new security group saa-alb-sg allowing HTTP 80 from anywhere. Listener HTTP:80 → forward to saa-tg. Tag Project=saa-lab, create.

Test it

When the state is Active and both targets are healthy (Target groups → Targets), open the ALB's DNS name in your browser and refresh several times. The hostname alternates between the two instances.

Fail an instance

Stop saa-web-1. In a minute its target shows unhealthy (then unused), and every refresh shows saa-web-2. Start it again and it rejoins.

Lock the instances down (optional)

Edit saa-web-sg: replace the HTTP rule's source 0.0.0.0/0 with the security group saa-alb-sg. The site still works through the ALB, but the instances' own public IPs no longer answer on port 80.

Expected result: one DNS name serving both instances in turn, and traffic moving away from a failed instance automatically.

Clean up: keep everything if you're going straight on to the ALB lab. Otherwise delete the load balancer saa-alb, then the target group saa-tg, terminate both instances, and delete saa-alb-sg.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt