Bird
Raised Fist0
AWScloud~7 mins

Listener rules and routing in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you have a load balancer, it needs to know where to send incoming traffic. Listener rules help decide which servers or services get the traffic based on conditions like the website address or path. This makes sure users reach the right part of your app.
When you want to send users to different backend servers based on the website address they visit.
When you have multiple services running and need to route traffic to the correct one using URL paths.
When you want to add rules that check for specific headers or hostnames to direct traffic.
When you want to manage traffic for multiple websites using one load balancer.
When you want to improve app performance by sending requests to the right service quickly.
Config File - listener-rules.json
listener-rules.json
{
  "ListenerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/6d0ecf831eec9f09",
  "Rules": [
    {
      "Priority": "10",
      "Conditions": [
        {
          "Field": "host-header",
          "HostHeaderConfig": {
            "Values": ["example.com"]
          }
        }
      ],
      "Actions": [
        {
          "Type": "forward",
          "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/73e2d6bc24d8a067"
        }
      ]
    },
    {
      "Priority": "20",
      "Conditions": [
        {
          "Field": "path-pattern",
          "PathPatternConfig": {
            "Values": ["/images/*"]
          }
        }
      ],
      "Actions": [
        {
          "Type": "forward",
          "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/image-targets/6d0ecf831eec9f09"
        }
      ]
    }
  ]
}

ListenerArn: The unique address of the load balancer listener where rules apply.

Rules: A list of rules with priorities to decide order.

Conditions: What to check in the request, like host name or URL path.

Actions: What to do if conditions match, usually sending traffic to a target group.

Commands
This command creates a listener rule that sends traffic to a target group when the host header matches example.com.
Terminal
aws elbv2 create-rule --listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/6d0ecf831eec9f09 --priority 10 --conditions Field=host-header,Values=example.com --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/73e2d6bc24d8a067
Expected OutputExpected
{ "Rules": [ { "RuleArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:rule/app/my-load-balancer/50dc6c495c0c9188/abcdef1234567890", "Priority": "10", "Conditions": [ { "Field": "host-header", "HostHeaderConfig": { "Values": [ "example.com" ] } } ], "Actions": [ { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/73e2d6bc24d8a067" } ] } ] }
→
--listener-arn - Specifies which listener the rule applies to
→
--priority - Sets the order of rule evaluation
→
--conditions - Defines when this rule matches incoming requests
This command adds a rule to send requests with paths starting with /images/ to a different target group.
Terminal
aws elbv2 create-rule --listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/6d0ecf831eec9f09 --priority 20 --conditions Field=path-pattern,Values=/images/* --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/image-targets/6d0ecf831eec9f09
Expected OutputExpected
{ "Rules": [ { "RuleArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:rule/app/my-load-balancer/50dc6c495c0c9188/123456abcdef7890", "Priority": "20", "Conditions": [ { "Field": "path-pattern", "PathPatternConfig": { "Values": [ "/images/*" ] } } ], "Actions": [ { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/image-targets/6d0ecf831eec9f09" } ] } ] }
→
--conditions - Defines the path pattern to match
→
--actions - Specifies forwarding to the correct target group
This command lists all rules for the listener so you can check what rules are active and their priorities.
Terminal
aws elbv2 describe-rules --listener-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/6d0ecf831eec9f09
Expected OutputExpected
{ "Rules": [ { "RuleArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:rule/app/my-load-balancer/50dc6c495c0c9188/abcdef1234567890", "Priority": "10", "Conditions": [ { "Field": "host-header", "HostHeaderConfig": { "Values": [ "example.com" ] } } ], "Actions": [ { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/73e2d6bc24d8a067" } ] }, { "RuleArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:rule/app/my-load-balancer/50dc6c495c0c9188/123456abcdef7890", "Priority": "20", "Conditions": [ { "Field": "path-pattern", "PathPatternConfig": { "Values": [ "/images/*" ] } } ], "Actions": [ { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/image-targets/6d0ecf831eec9f09" } ] } ] }
→
--listener-arn - Shows rules for this specific listener
Key Concept

If you remember nothing else from this pattern, remember: listener rules use conditions like host or path to send traffic to the right backend service.

Common Mistakes
Using the same priority number for multiple rules
AWS requires unique priorities to decide rule order; duplicates cause errors.
Assign a unique priority number to each listener rule.
Not specifying the correct listener ARN when creating rules
Rules won't apply if attached to the wrong listener, so traffic won't route as expected.
Always double-check and use the exact listener ARN for your load balancer.
Using incorrect condition fields or values, like misspelling 'host-header' or wrong path pattern syntax
Rules won't match requests, so traffic won't be routed correctly.
Use exact condition field names and valid values as per AWS documentation.
Summary
Create listener rules with unique priorities to route traffic based on host headers or path patterns.
Use the AWS CLI commands to add rules and verify them with describe-rules.
Listener rules help your load balancer send users to the right backend service depending on their request details.

Practice

(1/5)
1. What does a listener rule in AWS Elastic Load Balancer do?
easy
A. It monitors CPU usage of servers.
B. It creates new virtual machines automatically.
C. It stores data permanently for applications.
D. It directs incoming traffic to specific targets based on conditions.

Solution

  1. Step 1: Understand listener role

    A listener waits for incoming traffic on a load balancer and applies rules to route it.
  2. Step 2: Identify what rules do

    Listener rules check conditions like URL path or host header to send traffic to the right target group.
  3. Final Answer:

    It directs incoming traffic to specific targets based on conditions. -> Option D
  4. Quick Check:

    Listener rules route traffic [OK]
Hint: Listener rules route traffic based on conditions [OK]
Common Mistakes:
  • Confusing listener rules with server creation
  • Thinking listener rules store data
  • Mixing monitoring with routing
2. Which of the following is the correct way to specify a path pattern condition in an AWS listener rule?
easy
A. Method: 'POST'
B. PathPattern: '/images/*'
C. SourceIP: '192.168.1.1'
D. HostHeader: 'example.com'

Solution

  1. Step 1: Identify path pattern syntax

    Path patterns use wildcards like '*' to match URL paths, e.g., '/images/*'.
  2. Step 2: Match option to path pattern

    PathPattern: '/images/*' correctly shows a path pattern condition; others show different conditions.
  3. Final Answer:

    <code>PathPattern: '/images/*'</code> -> Option B
  4. Quick Check:

    Path pattern uses wildcard * [OK]
Hint: Path patterns use '*' wildcard for matching paths [OK]
Common Mistakes:
  • Confusing host header with path pattern
  • Using IP or method instead of path pattern
  • Missing quotes or wrong syntax
3. Given these listener rules with priorities:
1: HostHeader = 'app.example.com' -> TargetGroupA
2: PathPattern = '/api/*' -> TargetGroupB
What happens when a request comes to app.example.com/api/data?
medium
A. Traffic goes to TargetGroupB because path pattern matches better.
B. Traffic is rejected because two rules match.
C. Traffic goes to TargetGroupA because host header rule has higher priority.
D. Traffic goes to a default target group.

Solution

  1. Step 1: Check rule priorities

    Rules are checked in order of priority number; lower number means higher priority.
  2. Step 2: Evaluate matching rules

    Request matches both host header and path pattern, but host header rule has priority 1.
  3. Final Answer:

    Traffic goes to TargetGroupA because host header rule has higher priority. -> Option C
  4. Quick Check:

    Lower priority number wins routing [OK]
Hint: Lower priority number rules apply first [OK]
Common Mistakes:
  • Assuming path pattern overrides host header
  • Thinking multiple matches cause rejection
  • Ignoring rule priority order
4. You created a listener rule with priority 5 for path pattern /app/*, but traffic to /app/home is not routed correctly. What is a likely cause?
medium
A. Another rule with priority 1 matches the request first.
B. Path pattern syntax /app/* is invalid.
C. Listener rules do not support path patterns.
D. Target group is not attached to the load balancer.

Solution

  1. Step 1: Check rule priority impact

    Rules with lower priority numbers are evaluated first and can override later rules.
  2. Step 2: Consider other matching rules

    If a rule with priority 1 matches the request, the priority 5 rule won't be used.
  3. Final Answer:

    Another rule with priority 1 matches the request first. -> Option A
  4. Quick Check:

    Lower priority number rules block later ones [OK]
Hint: Check for higher priority rules blocking traffic [OK]
Common Mistakes:
  • Thinking path pattern syntax is wrong
  • Believing listener rules ignore path patterns
  • Assuming target group attachment causes routing failure
5. You want to route traffic to different target groups based on both host header and path pattern. Which setup correctly achieves this in AWS listener rules?
hard
A. Create a rule with priority 1: HostHeader = 'shop.example.com' AND PathPattern = '/products/*' -> TargetGroupShop
B. Create two separate rules with same priority: one for HostHeader, one for PathPattern
C. Use only one rule with PathPattern to cover all hosts
D. Set listener to forward all traffic to a single target group and filter inside the app

Solution

  1. Step 1: Understand combined conditions

    Listener rules can combine multiple conditions like host header AND path pattern in one rule.
  2. Step 2: Evaluate options

    Create a rule with priority 1: HostHeader = 'shop.example.com' AND PathPattern = '/products/*' -> TargetGroupShop correctly uses combined conditions with a single priority rule; others either conflict or do not filter properly.
  3. Final Answer:

    Create a rule with priority 1: HostHeader = 'shop.example.com' AND PathPattern = '/products/*' -> TargetGroupShop -> Option A
  4. Quick Check:

    Combine conditions in one rule for precise routing [OK]
Hint: Combine host and path in one rule for exact routing [OK]
Common Mistakes:
  • Using same priority for multiple rules causing conflicts
  • Relying on app filtering instead of load balancer routing
  • Using only path pattern ignoring host header
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard