Search code examples
amazon-web-servicesaws-cliaws-elb

Wildcards on query for AWS cli for elbv2


I'm trying to get the ARN of some LoadBalancer which I only know the prefix of the DNS name.

As an example, aws elbv2 describe-load-balancers --query 'LoadBalancers[].DNSName[]' will print me:

[
    "services-green-********.elb.eu-central-1.amazonaws.com",
    "services-blue-********.elb.eu-central-1.amazonaws.com"
]

Something like

aws elbv2 describe-load-balancers --query "LoadBalancers[?DNSName=='services-green-*']"

is not working because wildcards are not accepted and using filters like at ec2 commands is not possible.

How can I get the ARN without falling back to using jq?


Solution

  • You may try this;

    aws elbv2 describe-load-balancers --query 'LoadBalancers[?contains(DNSName, `services-green-*`) == `true`][DNSName]'
    

    You may remove the last [DNSName] if you want the full response. Or replace with DNSName with LoadBalancerArn;

    aws elbv2 describe-load-balancers --query 'LoadBalancers[?contains(DNSName, `services-green-*`) == `true`][LoadBalancerArn]'
    

    Just like contains, you can try starts_with if you don't want full wildcard but know how it starts.