Sub topic 5
Listeners and Routing Rules
This chapter assumes you know the basics of Application Load Balancers (ALBs) and target groups, and can run AWS CLI commands. It shows how a load balancer decides where each request goes.
What a listener does
A listener is a process on the load balancer that checks for connection requests on a protocol and port, such as HTTP on port 80 or HTTPS on port 443. A load balancer can have several listeners. Each listener holds an ordered set of rules.
NLB listeners work differently. They accept TCP, UDP, or TLS and have only a default action, usually forwarding to one target group. The rule-based routing below applies to ALBs.
Anatomy of a rule
Each ALB rule has three parts:
- Priority: a number from 1 to 50000. The load balancer evaluates rules from the lowest number up and stops at the first match.
- Conditions: what the request must look like, such as
host-header,path-pattern,http-header,query-string,http-request-method, orsource-ip. - Actions: what to do on a match. The common actions are
forward,redirect, andfixed-response.
Every listener also has a default rule. It has the lowest precedence, applies when no other rule matches, and cannot be deleted.
Creating rules
This command sends every request whose path starts with /api/ to a separate target group:
aws elbv2 create-rule \
--listener-arn "$LISTENER_ARN" \
--priority 10 \
--conditions Field=path-pattern,Values='/api/*' \
--actions Type=forward,TargetGroupArn="$API_TG_ARN"Requests that do not match go to the default action, the web-tg group from the earlier example. You can also change the default action. This one turns the port 80 listener into an HTTP-to-HTTPS redirect:
aws elbv2 modify-listener \
--listener-arn "$LISTENER_ARN" \
--default-actions 'Type=redirect,RedirectConfig={Protocol=HTTPS,Port=443,StatusCode=HTTP_301}'Common errors
- Wrong priority order. A broad rule such as
/*at priority 5 hides every more specific rule after it. Give specific rules lower numbers. - Duplicate priorities. Each rule on a listener needs a unique priority. A second
create-rulewith priority 10 fails. - Surprising 503 responses. A rule that forwards to a target group with no healthy targets returns HTTP 503.
- Unexpected 404 responses. Path patterns are case-sensitive, and
/api/*does not match/API/users. A fixed 404 response often comes from a default action configured asfixed-response. - Timeouts instead of responses. The load balancer's security group must allow inbound traffic on the listener port.
Exercise
A listener has rule 5 with path pattern /* forwarding to web-tg, and rule 10 with path pattern /api/* forwarding to api-tg. A request for /api/orders arrives. Which target group receives it, and how do you fix the problem?
Show answer
It goes to web-tg, because rule 5 has the lower number and matches every path, so rule 10 is never reached. Fix it by giving /api/* a lower priority number than the broad rule, or by deleting the /* rule and relying on the default action for everything else.