Sub topic 4
Application Load Balancer for HTTP and HTTPS traffic
This chapter assumes you can launch EC2 instances, know what a security group is, and understand subnets and Availability Zones (AZs). You should also be able to run AWS CLI commands. Here you will learn what an Application Load Balancer (ALB) does, how its parts fit together, and how to create one.
What an ALB does
An ALB works at layer 7, the application layer. It reads each HTTP or HTTPS request, then decides which target should receive it. It supports HTTP/1.1, HTTP/2, WebSockets and gRPC.
Because it understands requests, an ALB can route on content. A rule can match the host name, URL path, HTTP header, query string, HTTP method or source IP. A Network Load Balancer sees only TCP, UDP or TLS connections and cannot do this.
The ALB also ends the client connection and opens a new one to the target. This has two effects:
- The target sees the ALB's private IP address as the source. The original client address arrives in the
X-Forwarded-Forheader, and the original scheme arrives inX-Forwarded-Proto. - The ALB can reuse backend connections, apply idle timeouts (60 seconds by default), and terminate TLS for you.
The pieces
An ALB is built from four parts:
- The load balancer itself. It has a DNS name and places a node in each subnet you choose.
- One or more listeners. Each listener checks for connections on a protocol and port, such as HTTP on 80.
- Rules inside each listener. A rule maps conditions to an action. Every listener has a default rule.
- Target groups. A target group holds the instances, IP addresses or Lambda functions that receive traffic, plus the health check.
The DNS name resolves to the addresses of the ALB nodes. If one AZ fails, DNS stops returning that zone's node and traffic continues through the other zone.
Subnet and security group requirements
You must choose at least two subnets, each in a different AZ. Each subnet needs at least 8 free IP addresses, because the ALB uses them for its nodes and grows by adding nodes. AWS recommends a /27 or larger subnet.
For an internet-facing ALB, the subnets must be public: their route tables send 0.0.0.0/0 to an internet gateway. For an internal ALB, use --scheme internal. It gets only private addresses.
An ALB has its own security group. Configure two sets of rules:
- The ALB security group allows inbound 80 and 443 from the clients you want to serve.
- The target security group allows inbound traffic on the target port only from the ALB security group, not from
0.0.0.0/0.
Referencing the ALB's security group by ID keeps the rule correct even when the ALB's IP addresses change.
Creating an ALB with the CLI
The first command creates the load balancer and stores its ARN. The second creates a target group with a health check on /health. The third creates a listener that forwards every request to that target group.
ALB_ARN=$(aws elbv2 create-load-balancer \
--name web-alb \
--type application \
--scheme internet-facing \
--subnets subnet-aaaa1111 subnet-bbbb2222 \
--security-groups sg-0123456789abcdef0 \
--query 'LoadBalancers[0].LoadBalancerArn' \
--output text)
TG_ARN=$(aws elbv2 create-target-group \
--name web-tg \
--protocol HTTP \
--port 80 \
--vpc-id vpc-0123456789abcdef0 \
--target-type instance \
--health-check-path /health \
--query 'TargetGroups[0].TargetGroupArn' \
--output text)
aws elbv2 create-listener \
--load-balancer-arn "$ALB_ARN" \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn="$TG_ARN"The subnet, security group and VPC IDs are placeholders. Replace them with your own. The target group is empty until you register instances. You can do that by hand with aws elbv2 register-targets, or let an Auto Scaling group do it, which a later chapter covers.
To find the DNS name for testing, run this:
aws elbv2 describe-load-balancers \
--names web-alb \
--query 'LoadBalancers[0].DNSName' \
--output textCross-zone behavior
An ALB always has cross-zone load balancing enabled at the load balancer level. Each node can send requests to targets in any enabled AZ, so uneven instance counts between zones do not cause uneven load. You can turn cross-zone off for a single target group with the load_balancing.cross_zone.enabled attribute. Doing so is rarely useful for an ALB.
Related features
- Target types.
instanceregisters by instance ID and sends traffic to the primary network interface.ipregisters private IP addresses, which suits containers and on-premises servers reached over VPN or Direct Connect.lambdainvokes a function for each request. - HTTPS. Add a listener on 443 with an ACM certificate. The ALB decrypts traffic and can forward plain HTTP to the targets. A later chapter covers this.
- Idle timeout. If a connection has no data for 60 seconds, the ALB closes it. Raise the attribute for long-running requests, and keep the application's keep-alive timeout longer than the ALB's. Otherwise you may see intermittent 502 errors.
- Access logs. You can write request logs to S3. They are off by default.
- Deletion protection. An optional attribute that blocks accidental deletion.
Common errors
The ALB returns its own error codes, which point to where the problem is.
| Code | Typical cause |
|---|---|
| 502 Bad Gateway | The target closed the connection, sent an invalid response, or the keep-alive timeout is shorter than the ALB idle timeout |
| 503 Service Unavailable | The target group has no registered or healthy targets |
| 504 Gateway Timeout | The target did not respond within the idle timeout, or a security group or network ACL blocks the connection |
Exercise
An ALB sits in front of instances that log the source IP of each request. The logs show the same few private addresses for all clients. Why does this happen, and how can the application find the real client address?
Show answer
The ALB terminates the client connection and opens a new one to the target, so the target sees the ALB node's private IP as the source. The real client address is in the X-Forwarded-For header, which the application (or its web server's logging configuration) must read.
Exercise
You create an internet-facing ALB in two subnets, add a listener and a target group with two healthy instances, but requests to the DNS name time out. Name two configuration checks that could explain this.
Show answer
First, confirm the ALB's subnets are public (their route tables send 0.0.0.0/0 to an internet gateway). Second, confirm the ALB security group allows inbound traffic on the listener port from the client's address range. A security group on the targets that does not allow the ALB's security group would instead cause unhealthy targets and 502 or 503 errors.