AWS cli query to get to cloudfront “Domain Name” w

2019-05-12 01:14发布

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

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-05-12 02:02

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.

查看更多
登录 后发表回答