Search code examples
amazon-web-servicesamazon-cloudfrontaws-cli

AWS cli query to get to cloudfront "Domain Name" with specific origin name


This is my JSON output from awscli I want to get xxxxxxxx.cloudfront.net using Origin DomainName example1.com with AWS cli query only. { I know this filtering with jq, awk and cut, grep }.

"DistributionList": {
    "Items": [
        {
            "WebACLId": "", 
            "Origins": {
                "Items": [
                    {
                        "OriginPath": "", 
                        "CustomOriginConfig": {
                            "OriginProtocolPolicy": "http-only", 
                            "HTTPPort": 80, 
                            "HTTPSPort": 443
                        }, 
                        "Id": "DNS for Media Delivery", 
                        "DomainName": "example1.com"
                    }
                ], 
                "Quantity": 1
            }, 
            "DomainName": "xxxxxxxx.cloudfront.net", 
         },
        {
            "WebACLId": "", 
            "Origins": {
                "Items": [
                    {
                        "OriginPath": "", 
                        "CustomOriginConfig": {
                            "OriginProtocolPolicy": "http-only", 
                            "HTTPPort": 80, 
                            "HTTPSPort": 443
                        }, 
                        "Id": "DNS for Media Delivery", 
                        "DomainName": "example2.com"
                    }
                ], 
                "Quantity": 1
            }, 
            "DomainName": "yyyyyyyyyy.cloudfront.net", 
         },
       ]
    }

Solution

  • As AWS CLI --query parameter works on top of JMESPath you can build awesome filters. Answer for your question will be:

    --query "DistributionList.Items[].{DomainName: DomainName, OriginDomainName: Origins.Items[0].DomainName}[?contains(OriginDomainName, 'example1.com')] | [0]"
    

    and it will return you:

    {
      "DomainName": "xxxxxxxx.cloudfront.net",
      "OriginDomainName": "example1.com"
    }