Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 70 ยท Load balancing, scaling and delivery

Elastic Load Balancing

Learn AWS load balancers: ALB vs NLB vs GWLB, listeners, rules, target groups, health checks and TLS with ACM, plus runnable router and health-check simulators.

One server is a single point of failure and a hard ceiling on traffic. The usual answer is several identical servers behind a load balancer: clients talk to one stable address, and the balancer spreads requests over the servers that are alive. Elastic Load Balancing (ELB) is the AWS family of managed load balancers. AWS runs them across multiple Availability Zones, scales them for you and patches them, so you never log in to a balancer.

This lesson explains the three modern load balancer types, how requests flow from a listener through rules to a target group, how health checks remove broken servers, and how HTTPS works with ACM certificates. The balancers you create here sit in front of the instances from EC2 Instances and, in the next lesson, in front of an Auto Scaling Group.

Load balancers run inside AWS and cannot run on this site. The CLI commands and JSON are read-only. The Python examples are stand-ins: a rule router and a health-check simulator that follow the same logic in a simplified form. They do not model real networking, TLS or latency.

The three types

Type OSI layer Speaks Good for
Application Load Balancer (ALB) 7 HTTP, HTTPS, gRPC, WebSocket Web apps and APIs: routing by host, path, header, query string
Network Load Balancer (NLB) 4 TCP, UDP, TLS Very high throughput, low latency, static IP addresses, non-HTTP protocols
Gateway Load Balancer (GWLB) 3 IP packets wrapped in GENEVE (UDP 6081) Inserting third-party firewalls and inspection appliances into a traffic path

The older Classic Load Balancer still exists but is for legacy workloads. Do not use it for new designs.

Rules of thumb:

  • Serving a website or API over HTTP? Use an ALB. It understands requests, so it can send /api/* to one service and /images/* to another, redirect HTTP to HTTPS, or answer with a fixed response.
  • Need a fixed IP (or Elastic IP) per Availability Zone, UDP, millions of connections, or the client's source IP preserved without headers? Use an NLB. It does not look inside the traffic.
  • Need every packet to pass a security appliance first? Use a GWLB. You will probably meet it only in network security designs.

Listeners, rules and target groups

Three objects make a load balancer work:

  • A listener waits for connections on a protocol and port, such as HTTPS on 443.
  • A rule (ALB only) checks conditions and chooses an action. Every listener has a default rule, and extra rules have priorities from 1 to 50,000. The lowest priority number is checked first, and the first rule that matches wins.
  • A target group is a list of destinations plus a health check. Targets can be EC2 instances, IP addresses (including containers and on-premises servers), or Lambda functions. An ALB can forward to Lambda, and an NLB can forward to an ALB.
flowchart LR
    C["Client"] --> L["Listener<br/>HTTPS :443"]
    L --> R1{"Rule 10: host api.example.com"}
    R1 -- "match" --> TG1["Target group: api<br/>3 instances"]
    R1 -- "no" --> R2{"Rule 20: path /images/*"}
    R2 -- "match" --> TG2["Target group: images"]
    R2 -- "no" --> D["Default rule:<br/>forward to web"]
    D --> TG3["Target group: web"]

A request enters a listener, the rules are checked in priority order, and the first match decides the target group.

An ALB rule condition can be a host header, path pattern, HTTP header, query string, source IP, or HTTP method. The actions are forward (optionally weighted across several target groups for blue/green and canary releases), redirect, fixed-response, and authentication through Amazon Cognito or an OIDC identity provider.

Creating a path-based rule with the CLI looks like this (read-only):

Terminal
aws elbv2 create-rule \
  --listener-arn arn:aws:elasticloadbalancing:eu-west-1:123456789012:listener/app/web/50dc6c495c0c9188/f2f7dc8efc522ab2 \
  --priority 20 \
  --conditions Field=path-pattern,Values='/images/*' \
  --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/images/73e2d6bc24d8a067

The stand-in below follows the rule logic: sort by priority, evaluate every condition of a rule (all conditions must match, and values inside one condition are alternatives), and fall back to the default action. Path patterns and host headers support * and ? wildcards, as in the real service.

Python A listener-rule router

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first request shows why order matters: it matches rule 10 (the host) before rule 20 (the path) is looked at, so it goes to api. The admin path reaches the admin target group only when both conditions match (rule 30 needs the path and the header), otherwise it falls through to the default rule.

Health checks

A load balancer sends traffic only to healthy targets. Each target group has a health check:

  • The protocol, port and path to request (for an ALB, typically GET /health).
  • The interval between checks, and a timeout for each.
  • The healthy threshold: how many consecutive successes turn an unhealthy target healthy.
  • The unhealthy threshold: how many consecutive failures take a healthy target out of rotation.
  • For HTTP checks, the success codes (the default is 200).

A good health endpoint answers quickly and checks what the instance needs in order to serve requests, such as the process being up and its configuration loaded. Do not make it call every downstream dependency. If the database blips, every instance would fail its check at once and the balancer would have no healthy targets at all.

Two behaviors surprise newcomers:

  • Fail-open. If every target in a group is unhealthy, an ALB routes requests to all of them anyway, rather than refusing everything. Often a broken health check endpoint is the problem, and the app would still serve pages.
  • Deregistration delay. When a target is removed (a scale-in, a deployment), the balancer stops sending new requests but gives in-flight requests up to 300 seconds by default to finish. Lower it for short requests so deployments are faster.

When you use an Auto Scaling group, you can tell the group to use the load balancer's health result, so it replaces instances that the balancer marks as unhealthy.

The simulator below runs three targets through a series of probe results. It uses a healthy threshold of 3 and an unhealthy threshold of 2, spreads requests round-robin over healthy targets, and shows the fail-open case at the end.

Python A health-check simulator with thresholds

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In round 3, i-bbb drops out and the four requests rotate over i-aaa and i-ccc only. Three good rounds later it returns. In the last round all targets are down, and the balancer spreads requests over all three, which is the fail-open behavior.

Cross-zone balancing and the NLB differences

A load balancer has a node in each Availability Zone you enable. With cross-zone load balancing on, each node can send requests to targets in any zone, which spreads load evenly even when zones hold unequal numbers of instances. An ALB has it on by default, and you are not charged for the traffic between zones. An NLB has it off by default, and enabling it adds inter-zone data transfer charges.

Other NLB specifics: each zone gets a static IP address (or an Elastic IP you provide), it preserves the client's source IP for instance targets, and it can pass TLS through to the targets untouched (a TCP listener) or end TLS itself (a TLS listener). An NLB does not understand paths or hosts, so if you need those, use an ALB.

HTTPS and certificates with ACM

An HTTPS listener ends TLS at the load balancer, so your instances can serve plain HTTP inside the VPC and do not need certificates of their own. The certificate comes from AWS Certificate Manager (ACM): public certificates are free when used with ELB, validated through DNS (a CNAME record, easiest if the domain is in Route 53, see Route 53 and DNS), and renewed automatically.

  • You can attach several certificates to one listener. The balancer picks the right one with SNI (the client sends the host name in the TLS handshake).
  • A security policy chooses the allowed TLS versions and ciphers. Pick a recent one, such as a TLS 1.2 or TLS 1.3 policy, unless you must support very old clients.
  • Add a rule or a listener on port 80 that redirects to HTTPS (a redirect action with status 301).

A typical edge-to-instance setup keeps the instances in private subnets, the balancer in public subnets, and the instance security group allowing port 80 only from the balancer's security group, as covered in Connecting to Instances and Security Groups.

Creating an HTTPS listener (read-only):

Terminal
aws elbv2 create-listener \
  --load-balancer-arn arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/app/web/50dc6c495c0c9188 \
  --protocol HTTPS --port 443 \
  --ssl-policy ELBSecurityPolicy-TLS13-1-2-2021-06 \
  --certificates CertificateArn=arn:aws:acm:eu-west-1:123456789012:certificate/11111111-2222-3333-4444-555555555555 \
  --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/web/6d0ecf831eec9f09

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You want requests for /api/* to reach one set of containers and /shop/* another, behind one domain name. Which load balancer fits?

    Path-based routing is a layer 7 feature of the ALB. The NLB works at layer 4 and never looks at the URL path.
  2. 2. Two ALB rules both match a request. One has priority 10 and the other has priority 20. What happens?

    Rules are evaluated in ascending priority order and the first match wins. The default rule is used only when no other rule matches.
  3. 3. Every target in a target group fails its health check. What does an ALB do?

    When no target is healthy the ALB tries all of them, on the idea that the health check itself may be wrong and the application may still work.
  4. 4. Where is the TLS certificate for an HTTPS listener usually kept, and what does it cost for public certificates?

    ACM provides public certificates at no charge for use with ELB, validates them through DNS and renews them automatically.

Summary

  • ELB offers the Application (layer 7), Network (layer 4) and Gateway (appliance insertion) load balancers. Use an ALB for web traffic and an NLB for static IPs, UDP or extreme throughput.
  • A listener holds rules, and rules forward, redirect or answer with a fixed response. Priorities are checked from the lowest number up, and the first match wins before the default rule.
  • Target groups hold instances, IPs or Lambda functions, and their health checks (path, interval, thresholds, success codes) decide who receives traffic.
  • An ALB fails open when all targets are unhealthy, and deregistration delay lets in-flight requests finish.
  • Terminate TLS at the balancer with free ACM certificates, use SNI for several domains and redirect HTTP to HTTPS.

Next: Auto Scaling Groups