Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Route53 CLI list-resource-record-sets by Value

I need to locate a record in Route53 based on Value. My Route53 has 10,000+ records. Searching by Value for a Hosted Zone with more than 2000 records is not currently supported in the web interface. So, I must resort to using the AWS Route53 CLI's list-resource-record-sets command and the --query parameter. This parameter uses JMESPath to select or filter the result set.

So, let's look at the result set we are working with.

$ aws route53 list-resource-record-sets --hosted-zone-id  Z3RB47PQXVL6N2 --max-items 5 --profile myprofile
{
    "NextToken": "eyJTdGFydFJlY29yZE5hbWUiOiBudWxsLCAiU3RhcnRSZWNvcmRJZGVudGlmaWVyIjogbnVsbCwgIlN0YXJ0UmVjb3JkVHlwZSI6IG51bGwsICJib3RvX3RydW5jYXRlX2Ftb3VudCI6IDV9",
    "ResourceRecordSets": [
        {
            "ResourceRecords": [
                {
                    "Value": "ns-1264.awsdns-30.org."
                },
                {
                    "Value": "ns-698.awsdns-23.net."
                },
                {
                    "Value": "ns-1798.awsdns-32.co.uk."
                },
                {
                    "Value": "ns-421.awsdns-52.com."
                }
            ],
            "Type": "NS",
            "Name": "mydomain.com.",
            "TTL": 300
        },
        {
            "ResourceRecords": [
                {
                    "Value": "ns-1264.awsdns-30.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
                }
            ],
            "Type": "SOA",
            "Name": "mydomain.com.",
            "TTL": 300
        },
        {
            "ResourceRecords": [
                {
                    "Value": "12.23.34.45"
                }
            ],
            "Type": "A",
            "Name": "abcdefg.mydomain.com.",
            "TTL": 300
        },
        {
            "ResourceRecords": [
                {
                    "Value": "34.45.56.67"
                }
            ],
            "Type": "A",
            "Name": "zyxwvut.mydomain.com.",
            "TTL": 300
        },
        {
            "ResourceRecords": [
                {
                    "Value": "45.56.67.78"
                }
            ],
            "Type": "A",
            "Name": "abcdxyz.mydomain.com.",
            "TTL": 300
        }
    ]
}

Ideally I need to find the ResourceRecordSets.Name, but I can definitely work with returning the entire ResourceRecordSet object, of any record that has a ResourceRecords.Value == 45.56.67.78.

My failed attempts

// My first attempt was to use filters on two levels, but this always returns an empty array
ResourceRecordSets[?Type == 'A'].ResourceRecords[?Value == '45.56.67.78'][]
[]

// Second attempt came after doing more research on JMESPath. I could not find any good examples using filters on two levels, so I do not filter on ResourceRecordSets
ResourceRecordSets[*].ResourceRecords[?Value == '45.56.67.78']
[
    [],
    [],
    [
        {
            "Value": "45.56.67.78"
        }
    ],
    [],
    []
]

After beating my head on the desk for a while longer I decided to consult the experts. Using the above example, how can I utilize JMESPath and the AWS Route53 CLI to return one of the two following for records with a Value == 45.56.67.78?

[
    "Name": "abcdxyz.mydomain.com."
]

OR

{
    "ResourceRecords": [
        {
            "Value": "45.56.67.78"
        }
    ],
    "Type": "A",
    "Name": "abcdxyz.mydomain.com.",
    "TTL": 300
}
like image 595
Aaron St Clair Avatar asked Dec 23 '22 07:12

Aaron St Clair


1 Answers

This should do:

aws route53 list-resource-record-sets --hosted-zone-id Z3RB47PQXVL6N2 --query "ResourceRecordSets[?ResourceRecords[?Value == '45.56.67.78'] && Type == 'A'].Name"
like image 189
Dusan Bajic Avatar answered Dec 27 '22 03:12

Dusan Bajic