Application Load Balancer
The layer 7 load balancer for HTTP and HTTPS, with routing by path, host, headers and query string, target groups, and the X-Forwarded-For header.
What it is
The Application Load Balancer (ALB) works at layer 7, the HTTP level. Because it understands HTTP requests, it can route each one by its content:
- load balance to many HTTP applications across machines (target groups);
- load balance to many applications on the same machine, such as containers on different ports;
- supports HTTP/2, WebSocket and gRPC;
- redirects, for example HTTP to HTTPS, and fixed responses without touching a server;
- can authenticate users (Cognito or any OpenID Connect provider) before requests reach the app.
Routing to target groups
Each listener has rules, checked in priority order. A rule has conditions and an action. Conditions can match:
- the path in the URL:
example.com/usersvsexample.com/posts; - the hostname:
one.example.comvsother.example.com; - the query string:
example.com/users?id=123&order=false; - HTTP headers, the HTTP method (GET, POST) and the source IP.
Actions are forward (to one target group, or split by weight between several, handy for blue/green deployments), redirect, fixed response, and authenticate. Each listener also has a default rule for requests no other rule matches.
This makes ALBs a great fit for microservices and containers (Docker, Amazon ECS): one ALB serves many services. ECS uses dynamic port mapping, so many containers on one instance each register on their own port. Classic Load Balancers needed one load balancer per application.
Target groups
An ALB target group can contain:
- EC2 instances (often managed by an Auto Scaling group), over HTTP or HTTPS;
- ECS tasks (registered by ECS itself);
- Lambda functions: the HTTP request becomes a JSON event;
- IP addresses, which must be private IPs (for example on-premises servers reached over VPN or Direct Connect).
One ALB can route to many target groups, and health checks are set per target group. The default routing algorithm is round robin; least outstanding requests sends each request to the least busy target.
Good to know: the client's IP
- An ALB has a fixed DNS name (
name-123.region.elb.amazonaws.com), but no static IP addresses: point Route 53 or a CNAME at the name. - It terminates the client's connection and opens a new one to the target. The application sees the ALB's private IP as the source.
- The client's real IP is in the
X-Forwarded-Forheader, with the port inX-Forwarded-Portand the protocol inX-Forwarded-Proto.
Updated since the slides
ALBs now also support gRPC, routing by HTTP method and source IP, weighted forwarding between target groups, mutual TLS (clients present certificates too), and JWT verification of tokens at the load balancer. The core ideas in the slides are unchanged.
Exam tips
- "Route by URL path / hostname / query string / header" → ALB listener rules.
- "Microservices or containers behind one load balancer", "ECS dynamic ports" → ALB.
- "Invoke a Lambda function over HTTP from a load balancer" → ALB with a Lambda target group.
- "Redirect HTTP to HTTPS" → ALB redirect action.
- "The application needs the client's IP address" → read
X-Forwarded-For. - "Static IP for an ALB" → put an NLB in front with the ALB as its target, or use Global Accelerator.
Common traps
- "Give the ALB an Elastic IP." ALBs don't support Elastic IPs.
- "The app logs show the client IP as the source." They show the ALB's IP unless
X-Forwarded-Foris logged. - "Use an ALB for UDP traffic." ALB is HTTP/HTTPS only; UDP needs an NLB.
Hands-on lab: route by query string and return a fixed response
Goal: add listener rules that route some requests to a different target group and answer others without any server.
Costs money
Uses saa-alb and the two instances from the Elastic Load Balancing lab, under $0.10 an hour. Delete everything at the end.
Make a second target group
Create target group saa-tg-2 (instances, HTTP 80) and register only saa-web-2.
Add a query-string rule
Open saa-alb → Listeners → HTTP:80 → Manage rules → Add rule: name two, condition Query string key who, value two, action Forward to saa-tg-2, priority 10. Save.
Add a fixed-response rule
Add another rule: condition Path is /health-page, action Return fixed response, code 200, content type text/plain, body ALB is up. Priority 20.
Test
Browse to:
http://<ALB DNS>/: alternates between both instances (the default rule);http://<ALB DNS>/?who=two: always showssaa-web-2's hostname, however often you refresh;http://<ALB DNS>/health-page: the text ALB is up, served by the load balancer itself.
Expected result: the default rule balances between both instances, ?who=two always reaches one target group, and /health-page is answered by the load balancer alone.
Clean up: delete saa-alb, then both target groups, terminate both instances, and delete saa-alb-sg.
AWS documentation
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.
Network Load Balancer
The layer 4 load balancer for TCP, UDP, TLS and QUIC, with static IPs per AZ, extreme performance, and instance, IP or ALB targets.