Skip to content

Instantly share code, notes, and snippets.

@tfentonz
Last active June 13, 2023 10:44
Show Gist options
  • Save tfentonz/96d39dc021b382c3d58eb2f96f4605df to your computer and use it in GitHub Desktop.
Save tfentonz/96d39dc021b382c3d58eb2f96f4605df to your computer and use it in GitHub Desktop.
AWS CLI command to list ELB listener rule priorities

Get load balancer ARN

arn=$(aws elbv2 describe-load-balancers --names <value> --query 'LoadBalancers[].[LoadBalancerArn]' --output text)

Get listeners

aws elbv2 describe-listeners --load-balancer-arn "$arn" --query 'Listeners[].{ListenerArn:ListenerArn,Protocol:Protocol,Port:Port}'

[
    {
        "ListenerArn": "<listener_arn>",
        "Protocol": "HTTPS",
        "Port": 443
    },
    {
        "ListenerArn": "<listener_arn>",
        "Protocol": "HTTP",
        "Port": 80
    }
]

Describe rules

aws elbv2 describe-rules --listener-arn "<listener_arn>" --query 'Rules[].{Priority:Priority,Host:Conditions[0].Values[0]}'

[
    {
        "Priority": "1",
        "Host": "www.foo.com"
    },
    {
        "Priority": "2",
        "Host": "www.bar.com"
    },
    {
        "Priority": "5",
        "Host": "www.foobar.com"
    },
    {
        "Priority": "default",
        "Host": null
    }
]
@Woodz
Copy link

Woodz commented Sep 28, 2020

Thank you! Since we cannot view priority numbers via the AWS website, this comes in very useful!

@andrebask
Copy link

If you have jq installed you can make a script out of those commands

#!/bin/bash

lb_arn=$(aws elbv2 describe-load-balancers --names "$1" --query 'LoadBalancers[].[LoadBalancerArn]' --output text)

listener_arn=$(aws elbv2 describe-listeners --load-balancer-arn "$lb_arn" --query 'Listeners[].{ListenerArn:ListenerArn,Protocol:Protocol,Port:Port}' | jq -r ".[] | select(.Port=="$2").ListenerArn")

aws elbv2 describe-rules --listener-arn "$listener_arn" --query 'Rules[].{Priority:Priority,Host:Conditions[0].Values[0]}' | jq

You can run this passing the name of the load balancer and the listener port you want to show the rules for.

./get_lb_priorities.sh web-app-test 443

@xx745
Copy link

xx745 commented Sep 15, 2022

Old but good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment