On an AWS Application Load Balancer, what can a listener rule match on, and how does the load balancer decide which rule applies to a given request?
answer
- conditions on the request, ordered list
- priority number decides, not specificity
- first match wins, then default action
- AND across conditions, OR within one
- source-ip is the connection, not the header
basics
~20 sAn ALB listener rule matches on host header, URL path, HTTP header, HTTP method, query string, or source IP. Rules are evaluated in priority order, lowest number first; the first match wins, and unmatched requests fall through to the listener's default action.
solid answer
~40 sEvery ALB listener has a default action plus an ordered list of rules. Each rule carries a priority number and one or more conditions: `host-header`, `path-pattern`, `http-header`, `http-request-method`, `query-string`, or `source-ip`. Conditions inside one rule are ANDed - all must match - while several values inside a single condition are ORed. The ALB walks the rules from the lowest priority number upward and applies the **first** one that matches; there is no most-specific-wins logic, so ordering is the design. If nothing matches, the listener's default action runs. Actions are `forward` to one or more target groups (optionally weighted), `redirect`, `fixed-response`, `authenticate-oidc` or `authenticate-cognito`. Two gotchas: path patterns are case-sensitive and do not include the query string, and `source-ip` matches the address of the connection, not anything in `X-Forwarded-For`.
code
bash · 9 linesaws elbv2 create-rule \
--listener-arn "$LISTENER_ARN" \
--priority 20 \
--conditions Field=host-header,Values='api.example.com' \
Field=path-pattern,Values='/v2/*' \
--actions Type=forward,TargetGroupArn="$TG_ARN"
aws elbv2 describe-rules --listener-arn "$LISTENER_ARN" \
--query 'Rules[].{Priority:Priority,Conditions:Conditions[].Field}'go deeper
Be able to list what a rule can match on - host, path, header, method, query string, source IP - and state plainly that rules are tried in priority order and the first match wins.
Explain AND across conditions versus OR within one, why a broad rule at a low priority silently shadows narrower ones, and what the non-forward actions (redirect, fixed-response) are for.
Demonstrate the operational angles: weighted forward actions for canaries, a fixed-response default that fails closed on unknown hosts, and why the source-ip condition is the wrong tool behind a CDN.
Own the routing convention for the whole platform - host-based versus path-based, who may edit the rule table, and how you stay clear of the rules-per-load-balancer quota as service count grows.
## The shape of a listener An Application Load Balancer has one or more listeners, each bound to a port and protocol. A listener always has a **default action** - what happens when no rule matches - and may have any number of **rules** layered on top of it. This is the entire routing model: there is no separate route table, no server blocks, no virtual-host config. Everything about which service receives a request is expressed as rules on a listener. ## Conditions A rule has between one and several conditions, and the ALB supports exactly these condition fields: - `host-header` - the value of the HTTP `Host` header (or the `:authority` pseudo-header on HTTP/2). Wildcards are allowed, so `*.example.com` works. - `path-pattern` - the URL path only. `/api/*` matches `/api/users`; it does **not** see `?page=2`, and matching is case-sensitive, so `/API/users` will not match `/api/*`. - `http-header` - any named request header and a list of values to match, which is how you route on a custom header such as a client or tenant identifier. - `http-request-method` - `GET`, `POST`, and so on, so you can send writes somewhere different from reads. - `query-string` - key/value pairs from the query. - `source-ip` - the source address of the connection, given in CIDR form. Within one rule, all conditions must be satisfied - they are ANDed. Within one condition, the listed values are alternatives - they are ORed. So a single rule can say "host is `api.example.com` **and** path starts with `/v2/`", but expressing "path `/a/*` **or** path `/b/*` routed to different groups" needs two rules. ## Evaluation order Each rule has a numeric priority. The load balancer evaluates rules in ascending priority order and stops at the first rule whose conditions all match. There is no longest-prefix or most-specific tiebreak, which is the single most common surprise: if a broad rule such as `path-pattern: /*` sits at priority 10 and a narrow one such as `/admin/*` sits at priority 20, the narrow one is dead code and every admin request goes wherever the broad rule pointed. Order rules from most specific to least specific, and leave gaps between priority numbers so you can insert a rule later without renumbering. If no rule matches, the listener's default action runs. A common pattern is to make the default a `fixed-response` returning 404, so an unknown host cannot accidentally land on whichever service happens to be first. ## Actions - `forward` sends the request to a target group; the forward action can also list several target groups with weights, which is how blue/green and canary shifting is done at the load balancer. - `redirect` answers the request itself with a 301 or 302 and can substitute pieces of the original URL, which is how the port-80 listener sends everyone to HTTPS without any backend. - `fixed-response` returns a status code, content type and short body straight from the load balancer - a maintenance page or a cheap rejection. - `authenticate-oidc` / `authenticate-cognito` run a login flow before the request is forwarded; when present they must come first in the rule's action list, with the forward last. ## The source-IP trap The `source-ip` condition matches the address the load balancer sees on the connection. If your ALB sits behind CloudFront or any other proxy, that address belongs to the proxy, not the end user, and a rule intended to allow only the office network will either match nothing or match everyone. The correct instrument in that topology is an `http-header` condition on the forwarded-client-address header - with the understanding that a header can be forged unless the edge in front of you is trusted and overwrites it. ## Quotas shape the design Rules per load balancer are capped by a service quota (adjustable through Service Quotas, but not unlimited), and rule evaluations are one of the billing dimensions of an ALB's capacity units. A design that gives every microservice fifteen path rules on one shared listener will meet that ceiling; grouping by host header, with one rule per service, scales much further. ```bash aws elbv2 create-rule \ --listener-arn "$LISTENER_ARN" \ --priority 20 \ --conditions Field=host-header,Values='api.example.com' \ Field=path-pattern,Values='/v2/*' \ --actions Type=forward,TargetGroupArn="$TG_ARN" ``` Both conditions must hold for this rule to fire, and any request that matches a rule with a priority below 20 never reaches it.
- You add a rule for /admin/* but every admin request still lands on the main app. What do you check first?The priority numbers. A broader rule - often a catch-all `/*` - is sitting at a lower priority and matching first, because the ALB stops at the first match and never prefers the more specific pattern. Move the `/admin/*` rule to a lower number, or narrow the catch-all.
- How would you send 5% of traffic to a new version without touching DNS?Use a single `forward` action listing both target groups with weights, for example 95 and 5. The ALB splits requests by weight, and you shift the weights as confidence grows. Enable target-group stickiness on that forward action if a user must stay on one version for their whole session.
- Your ALB sits behind CloudFront and a source-ip rule meant to restrict an internal path matches nothing useful. Why?CloudFront terminates the client connection, so the address the ALB sees is a CloudFront node, not the end user. Match on the forwarded-client-address header instead, and make sure only the edge you trust can reach the ALB - otherwise a caller can set that header themselves.
saying these in an interview costs you the question
- The ALB picks the most specific matching rule.
- Path patterns include the query string.
- Path matching is case-insensitive.
- source-ip conditions read the X-Forwarded-For header.
- A rule with no match falls through to the next rule instead of the default action.