Sub topic 3
Application Load Balancer for HTTP and HTTPS traffic
This chapter assumes you know basic EC2 instances and security groups, and that you understand subnets and Availability Zones in a VPC. You should be able to run AWS CLI commands.
What an Application Load Balancer does
An Application Load Balancer (ALB) works at layer 7 of the network model. It reads HTTP and HTTPS requests, so it can make routing decisions based on the content of each request, such as the host name, the URL path, headers, or query strings. A Network Load Balancer sees only TCP or UDP connections and cannot do this.
An ALB gives you one DNS name. It places a load balancer node in each subnet you choose, and clients reach those nodes. The nodes forward each request to a healthy target in a target group.
Three building blocks
An ALB is made of three resources that you create in order:
- The load balancer holds the subnets and security groups.
- The target group holds the registered targets and the health check settings.
- The listener checks for connections on a protocol and port, and forwards them using its rules.
Creating an ALB
The following commands create an internet-facing ALB in two subnets, a target group that checks /health, and an HTTP listener on port 80. Set ALB_ARN and TG_ARN from the LoadBalancerArn and TargetGroupArn values in the output of the first two commands.
aws elbv2 create-load-balancer \
--name web-alb \
--type application \
--scheme internet-facing \
--subnets subnet-aaaa1111 subnet-bbbb2222 \
--security-groups sg-0123456789abcdef0
aws elbv2 create-target-group \
--name web-tg \
--protocol HTTP \
--port 80 \
--vpc-id vpc-0123456789abcdef0 \
--target-type instance \
--health-check-path /health
aws elbv2 create-listener \
--load-balancer-arn "$ALB_ARN" \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn="$TG_ARN"Each choice has a purpose:
- Two subnets in different Availability Zones keep the ALB available if one zone fails. An ALB needs at least two.
internet-facinggives the nodes public addresses. Aninternalscheme gives private addresses only.- The security group on the ALB must allow inbound traffic on the listener port. The security group on your instances should allow traffic from the ALB's security group, not from the whole internet.
Features specific to the ALB
- Content-based routing. Rules on a listener can send
/api/*to one target group and/images/*to another. - HTTPS. An HTTPS listener can terminate TLS with a certificate from AWS Certificate Manager. Later chapters cover this.
- Client address. Targets see the ALB's address as the source. The original client address arrives in the
X-Forwarded-Forheader. - Idle timeout. The ALB closes a connection after 60 seconds of inactivity by default. Long-running requests may need a higher value.
Common errors
- 503 Service Unavailable. The target group has no registered targets, or none are healthy.
- 504 Gateway Timeout. A target did not respond before the timeout, often because its security group blocks the ALB.
- 502 Bad Gateway. A target closed the connection or sent an invalid response.
- Targets stay unhealthy. The health check path returns something other than a 200 response, or the port is wrong.
Exercise
Your ALB returns 504 errors, and the instances run a healthy web server. Which security group rule should you check first?
Show answer
Check that the instances' security group allows inbound traffic on the target port from the ALB's security group. If the ALB cannot reach the targets, requests time out.