AWS CLI and JMESPath

I want to use the CLI tool to get the identifier of the CloudFront distribution distribution with the specific name cname / alias.

Here is what I came up with:

aws cloudfront list-distributions --query "DistributionList.Items[?Aliases.Items!='null']|DistributionList.Items[?contains(Aliases.Items,'cname.cdn.mycompany.com') == 'true'].{Id:Id}"

I am not an expert at JMESPath, and I do not understand why my query does not return a result. A distribution with the specified domain as an alias exists.

+4
source share
1 answer

You are very close! Few things:

  • null not 'null'
  • After working with the pipe, you will work with the results of the first expression
  • like null, is truenot a string ( == truealso redundant)

jmespath.org , . json, :

{
    "DistributionList": {
        "Items": [
            {
                "Id": "foo",
                "Aliases": {
                    "Quantity": 1,
                    "Items": [
                        "cname.cdn.mycompany.com"
                    ]
                }
            },
            {
                "Id": "bar",
                "Aliases": {
                    "Quantity": 1,
                    "Items": [
                        "cname.cdn.othercompany.com"
                    ]
                }
            },
            {
                "Id": "baz",
                "Aliases": {
                    "Quantity": 0
                }
            }
        ]
    }
}

. , , - :

DistributionList.Items[?Aliases.Items!=null]

Items <<28 > . , , :

[
  {
    "Id": "foo",
    "Aliases": {
      "Quantity": 1,
      "Items": [
        "cname.cdn.mycompany.com"
      ]
    }
  },
  {
    "Id": "bar",
    "Aliases": {
      "Quantity": 1,
      "Items": [
        "cname.cdn.othercompany.com"
      ]
    }
  }
]

, DistributionList.Items.

, CNAME. == true.

[?contains(Aliases.Items, 'cname.cdn.mycompany.com')]

, :

DistributionList.Items[?Aliases.Items!=null] | [?contains(Aliases.Items, 'cname.cdn.mycompany.com')]

- :

[
  {
    "Id": "foo",
    "Aliases": {
      "Quantity": 1,
      "Items": [
        "cname.cdn.mycompany.com"
      ]
    }
  }
]

, , .Id . , , [0].

DistributionList.Items[?Aliases.Items!=null] | [?contains(Aliases.Items, 'cname.cdn.mycompany.com')].Id | [0]

, !

"foo"
+9

Source: https://habr.com/ru/post/1630036/


All Articles