amazon-web-servicesamazon-cloudfrontaws-cli

AWS CLI to get all matching CloudFronts using the "--query" option?


I use the AWS list-distributions CLI command to get all the ClodFronts in our system. For example

$ aws cloudfront list-distributions
{
    "DistributionList": {
        "Items": [
            {
                "Id": "E2DMQDAA2UTH",
                "ARN": "arn:aws:cloudfront::0123456789:distribution/E2DMQDAA2UTH",
                "Status": "Deployed",
                "LastModifiedTime": "2024-07-08T09:31:24.859000+00:00",
                "DomainName": "abcdefghijk.cloudfront.net",
                "Aliases": {
                    "Quantity": 1,
                    "Items": [
                        "sandbox.images.domain.com"
                    ]
                },
                "Origins": {
                ...
                },
                ...
            },
            {
                // more items
            }
        }
    }
}

What is the CLI command to use the --query option to get all DistributionList.Items.Aliases that contain both sandbox and domain.com? I've tried the following

aws cloudfront list-distributions --query 'DistributionList.Items[?contains(Aliases.Items[*], `sandbox`) && contains(Aliases.Items[*], `domain.com`)].[{Id:Id}]' --output json

But I get

In function contains(), invalid type for value: None, expected one of: ['array', 'string'], received: "null"

What is the correct syntax for what I want to do? I know I can write a pipeline/Groovy script to filter things out, but I don't want to do that.


Solution

  • Issue is that you are passing Aliases.Items[*] as an argument which is an array. However, in contains if we want to find an element in an array then we have to pass each element of the array for comparison using @.

    Here's the modified command,

    aws cloudfront list-distributions --query 'DistributionList.Items[?Aliases.Items[?contains(@, `sandbox`) && contains(@, `domain.com`)]].[{Id:Id}]' --output json