Listener Rules and Path-Based Routing
Write listener rules on the ALB to route requests to different target groups based on host headers, path patterns, or query strings.
Listener Rules and Path-Based Routing is a free AWS Solutions Architect lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
ALB Listeners Explained
An ALB listener is a process that checks for connection requests using a protocol and port you specify (e.g., HTTP on port 80 or HTTPS on port 443). Each listener has one or more rules that determine where to forward requests based on their content.
A listener must have a default rule (the catch-all action when no other rule matches) and can have up to 100 additional rules. Rules are evaluated in priority order (lowest number = highest priority). When a request matches a rule's condition, the corresponding action is applied and no further rules are evaluated.
# Create an HTTP listener on port 80
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:123456789:loadbalancer/app/my-alb/abc123 \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789:targetgroup/default-tg/def456Rule Conditions
Listener rules match requests based on conditions. You can combine multiple conditions in one rule (all conditions must match for the rule to apply). Available condition types:
- host-header: matches the
HostHTTP header (e.g.,api.example.com) - path-pattern: matches the URL path (e.g.,
/api/*,/images/*.jpg) - http-header: matches any HTTP header name and value pattern
- http-request-method: matches HTTP methods (GET, POST, DELETE, etc.)
- query-string: matches key-value pairs in the query string
- source-ip: matches client IP CIDR ranges
Path-Based Routing
Path-based routing directs requests to different target groups based on the URL path. This is the most common routing pattern for microservices behind a single ALB. Example rules on one ALB:
- Path is
/api/*→ Target Group: api-service - Path is
/images/*→ Target Group: image-processor - Path is
/admin/*→ Target Group: admin-app - Default → Target Group: frontend-app
This allows a single ALB to front multiple distinct services without requiring multiple load balancers, reducing cost and DNS complexity.
# Create a path-based routing rule
aws elbv2 create-rule \
--listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789:listener/app/my-alb/abc123/lis456 \
--priority 10 \
--conditions Field=path-pattern,Values='/api/*' \
--actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789:targetgroup/api-service/xyz789Host-Based Routing
Host-based routing routes requests based on the HTTP Host header, enabling multiple domain names (virtual hosts) to be served from a single ALB. Example:
- Host is
api.example.com→ api-service target group - Host is
admin.example.com→ admin-app target group - Host is
www.example.com→ frontend target group
Each domain name has its CNAME or ALIAS record pointing to the same ALB DNS name, but the ALB routes each to the appropriate backend based on the host header. Host-based routing is ideal for multi-tenant SaaS or monolithic apps being split into microservices.
# Create a host-based routing rule
aws elbv2 create-rule \
--listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789:listener/app/my-alb/abc123/lis456 \
--priority 5 \
--conditions '[{"Field":"host-header","HostHeaderConfig":{"Values":["api.example.com"]}}]' \
--actions '[{"Type":"forward","TargetGroupArn":"arn:aws:elasticloadbalancing:us-east-1:123456789:targetgroup/api-service/xyz789"}]'Rule Actions
When a rule's conditions match, the ALB executes one of these actions:
- forward: forward the request to one or more target groups (with optional weights)
- redirect: return an HTTP redirect (301 or 302) to a new URL; useful for HTTP to HTTPS redirects
- fixed-response: return a static HTTP response with a specified status code, content type, and body—useful for maintenance pages or simple health check responses
- authenticate-cognito: authenticate users via a Cognito User Pool before forwarding
- authenticate-oidc: authenticate users via any OIDC-compatible identity provider
# Create a redirect rule: HTTP to HTTPS
aws elbv2 create-rule \
--listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789:listener/app/my-alb/abc123/http80 \
--priority 1 \
--conditions '[{"Field":"path-pattern","PathPatternConfig":{"Values":["/*"]}}]' \
--actions '[{"Type":"redirect","RedirectConfig":{"Protocol":"HTTPS","Port":"443","StatusCode":"HTTP_301"}}]'HTTP to HTTPS Redirect Pattern
The most common listener rule pattern is redirecting HTTP to HTTPS:
- Create an HTTP listener on port 80 with one rule: redirect all traffic (
/*) to HTTPS with a 301 status code - Create an HTTPS listener on port 443 with your actual routing rules pointing to target groups
This ensures users who type http:// or follow old HTTP links are transparently redirected to HTTPS without any application-level changes. The redirect is handled entirely at the load balancer layer.
Fixed-Response Action
The fixed-response action returns a static HTTP response from the ALB without forwarding the request to any target. Use it for:
- Returning a 503 maintenance page for specific paths during maintenance
- Providing a lightweight health check endpoint directly from the ALB (returns 200 OK instantly without backend overhead)
- Blocking specific paths with a 403 Forbidden response
Fixed responses are useful for temporarily taking paths out of service without modifying application code or redeploying, by adjusting listener rules dynamically.
# Return 503 maintenance page for a specific path
aws elbv2 create-rule \
--listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789:listener/app/my-alb/abc123/lis456 \
--priority 20 \
--conditions '[{"Field":"path-pattern","PathPatternConfig":{"Values":["/checkout/*"]}}]' \
--actions '[{"Type":"fixed-response","FixedResponseConfig":{"StatusCode":"503","ContentType":"text/html","MessageBody":"<h1>Maintenance</h1>"}}]'ALB Authentication with Cognito
The ALB's authenticate-cognito action integrates with Amazon Cognito User Pools to handle user authentication before forwarding requests to your application. When an unauthenticated user reaches a protected listener rule, the ALB redirects them to the Cognito-hosted UI for login. After successful authentication, the ALB sets an encrypted cookie and forwards the request with user identity headers.
This offloads authentication logic entirely from your application. Your backend receives X-Amzn-Oidc-Identity, X-Amzn-Oidc-Data, and X-Amzn-Oidc-Access-Token headers with the authenticated user's claims.
Query String and Header-Based Routing
ALB listener rules can route based on query string parameters and HTTP headers, enabling fine-grained request routing:
- Route mobile clients by detecting
User-Agent: *Mobile*header to a mobile-optimised backend - Route premium API requests by checking a custom
X-API-Tier: premiumheader to a faster target group - Route A/B test variants by reading a query string parameter
?variant=beta
Header-based routing allows you to implement feature flags and traffic splitting at the infrastructure layer without modifying application code.
Rule Priority and Evaluation Order
Rules are evaluated in ascending priority order. Lower priority numbers are evaluated first. The first matching rule's action is applied and no further rules are evaluated. The default rule has no priority number and is always evaluated last as the catch-all.
Best practice: assign priority numbers in increments of 10 (10, 20, 30...) rather than sequential integers. This leaves room to insert new rules between existing ones without renumbering. More specific rules (e.g., path + host header) should have lower numbers (higher priority) than generic rules.
# List rules for a listener (shows priorities)
aws elbv2 describe-rules \
--listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789:listener/app/my-alb/abc123/lis456 \
--query 'Rules[*].{Priority:Priority,Conditions:Conditions[0].Field,Actions:Actions[0].Type}' \
--output tableListener Rules for Microservices
A single ALB can front an entire microservices platform using listener rules. A real-world example with all three condition types combined:
- Priority 10: Host=
api.example.com+ Path=/v2/*→ api-v2 target group - Priority 20: Host=
api.example.com+ Path=/v1/*→ api-v1 target group - Priority 30: Host=
auth.example.com→ auth-service target group - Priority 40: Host=
www.example.com+ Path=/static/*→ CloudFront redirect - Default: Host=
www.example.com→ frontend target group
This design reduces costs by eliminating the need for separate load balancers per service.
Quick Check
Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.
Lesson Recap
In this lesson you learned: listener rules route requests based on host, path, headers, methods, and query strings, rules are evaluated in priority order with the first match winning, and actions include forward, redirect, fixed-response, and Cognito authentication. Path and host-based routing enable a single ALB to front multiple microservices. Next up we explore SSL termination and sticky sessions.
Frequently asked questions
Is the “Listener Rules and Path-Based Routing” lesson free?
Yes — the full text of “Listener Rules and Path-Based Routing” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.
What will I learn in “Listener Rules and Path-Based Routing”?
Write listener rules on the ALB to route requests to different target groups based on host headers, path patterns, or query strings. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Solutions Architect?
No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Listener Rules and Path-Based Routing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Solutions Architect lesson?
Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- ALB vs NLB vs GLB: When to Use Which
- Target Groups and Health Checks
- Listener Rules and Path-Based Routing
- SSL Termination and Sticky Sessions