Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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", 
         },
       ]
    }
like image 425
Abhijit Avatar asked Dec 16 '15 22:12

Abhijit


1 Answers

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"
}

P.S. Hope it will help someone.

like image 138
D.Dimitrioglo Avatar answered Sep 21 '22 03:09

D.Dimitrioglo