5

I'm trying to find a load balancer that has a Name tag with some value.

aws elb describe-load-balancers --query 'LoadBalancerDescriptions[*].LoadBalancerName'

I'm iterating over the results and running:

aws elb describe-tags \
--load-balancer-names some-load-balancer \
--query 'TagDescriptions[?Tags[?Key==`Name`].Value == `my-desired-name-value`]'

The result is always empty even though this:

aws elb describe-tags \
--load-balancer-names some-load-balancer \
--query 'TagDescriptions[].Tags[?Key==`Name`].Value'

Does return my-desired-name-value. I'd like to get the entire object of the tags, using only JMESPath, I can't use jq here.

Desired output:

{
    "TagDescriptions": [
        {
            "LoadBalancerName": "some-load-balancer",
            "Tags": [
                {
                    "Key": "SomeTag",
                    "Value": "SomeValue"
                },
                {
                    "Key": "Name",
                    "Value": "my-desired-name-value"
                }
            ]
        }
    ]
}

What's wrong with my JMESPath query?

Moshe
  • 155
  • 1
  • 7

2 Answers2

5

You're trying to use --query to perform the role of --filter and unfortunately describe-tags does not support the --filter option.

The --query option allows you to select what fields are returned in the response. When available --filter allows you select which resources you want returned. It's described in more detail here

You can use jq to perform the function of the filter. I highly recommend it because AWS does not implement the --filter option for all CLI commands.

For your example try something like:

aws elb describe-tags --load-balancer-names some-load-balancer \
| jq -r '.TagDescriptions[] |select (.Tags[].Value=="my-desired-name-value")'

A bit more complex is to filter on the Tag Key and Value:

aws elb describe-tags --load-balancer-names some-load-balancer \
| jq -r '.TagDescriptions[] | . as $i \
| (select ($i.Tags[].Value=="my-desired-name-value")) and (select ($i.Tags[].Key=="Name")) \
| $i'

Output

{
  "LoadBalancerName": "some-load-balancer",
  "Tags": [
    {
      "Key": "Type",
      "Value": "classic"
    },
    {
      "Key": "Name",
      "Value": "my-desired-name-value"
    }
  ]
}

References

slm
  • 7,615
  • 16
  • 56
  • 76
kenlukas
  • 3,101
  • 2
  • 16
  • 26
-2

aws elbv2 describe-tags --resource-arns | jq -r '.TagDescriptions[] | . as $i | (select($i.Tags[].Value | test(".-.-05-myteam.-."))) and (select ($i.Tags[].Key=="Name")) | $i.ResourceArn'

  • You can use the regular expression to filter the Name tag as above using test(regex) – Padma Priyanjith Mar 23 '21 at 12:12
  • 1
    [Brevity is acceptable, but fuller explanations are better](https://serverfault.com/help/how-to-answer). Furthermore, asker already said they can't use `jq`. – mforsetti Mar 23 '21 at 13:20