Bhavik Joshi
Bhavik Joshi

Reputation: 2677

How to get the HostedZoneId of a HostedZone in As route53 with same name?

I have 2 hosted zone with the same name. I want to get the hostedZoneId of a Hostedzone used for us-west-2 region.

aws route53 list-hosted-zones-by-name --dns-name domainname

It gives the following output:

{
    "HostedZones": [
        {
            "ResourceRecordSetCount": 3, 
            "CallerReference": "2018-08-07T14:02:30.733383821+05:30", 
            "Config": {
                "Comment": "Private Hosted Zone for tenant:us-west-2", 
                "PrivateZone": true
            }, 
            "Id": "/hostedzone/D2JGX0PDINSIDA", 
            "Name": "domainname."
        }, 
        {
            "ResourceRecordSetCount": 3, 
            "CallerReference": "2018-08-16T16:38:29.821900042+05:30", 
            "Config": {
                "Comment": "Private Hosted Zone for tenant:eu-west-1", 
                "PrivateZone": true
            }, 
            "Id": "/hostedzone/Q1HEEHGD5JH3G3", 
            "Name": "domainname."
        }
    ], 
    "DNSName": "domainname", 
    "IsTruncated": false, 
    "MaxItems": "100"
}

As you can see there are two records for the same name, I want to get the Id of a hostedZone used for us-west-2. I dont have any uniqueness now to identify the HostedZone used for Us other than the Comment.

I tried with jq but I am not aware of how to provide conditions to it.

aws route53 list-hosted-zones-by-name --dns-name domainname | jq ".HostedZones | .[] | .Config"

Any help would be appreciated or any references

Upvotes: 3

Views: 585

Answers (1)

Inian
Inian

Reputation: 85550

It is a simple filter on jq to use endswith or test to match us-west-2 on the .Config.Comment field value. (See it working on jqplay.org )

jq '.HostedZones[] | select( .Config.Comment | test("us-west-2$") ).Id'

As ever, to remove the outer quotes, use the --raw-output mode with jq -r ..

Upvotes: 4

Related Questions