0
0
AWScloud~7 mins

Listener rules and routing in AWS - Commands & Configuration

Choose your learning style9 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.