Search code examples
phpamazon-web-servicesamazon-route53aws-php-sdk

AWS Route 53 - Get hosted zone by domain name?


Is there a way to retrieve a Hosted Zone by Domain Name ?

I thought using listHostedZonesByName( 'pagefoo.io' ) would do the trick, but it returns unrelated results: pagefoo.net, pagefoo.org instead of just pagefoo.io.

Array
(
    [0] => Array
        (
            [Id] => /hostedzone/XXXXXXXXXXXX
            [Name] => pagefoo.io.
            [CallerReference] => XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
            [Config] => Array
                (
                    [PrivateZone] => 
                )

            [ResourceRecordSetCount] => 5
        )

    [1] => Array
        (
            [Id] => /hostedzone/XXXXXXXXXXXX
            [Name] => pagefoo.net.
            [CallerReference] => XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
            [Config] => Array
                (
                    [PrivateZone] => 
                )

            [ResourceRecordSetCount] => 6
        )

    [2] => Array
        (
            [Id] => /hostedzone/XXXXXXXXXXXX
            [Name] => pagefoo.org.
            [CallerReference] => XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
            [Config] => Array
                (
                    [PrivateZone] => 
                )

            [ResourceRecordSetCount] => 4
        )

)

Solution

  • Using the aws-cli route53 interface, I called list-hosted-zones-by-name. It returned all of my zones.

    $ aws route53 list-hosted-zones-by-name
    {
        "HostedZones": [
            (lots of output here)
        ], 
        "IsTruncated": false, 
        "MaxItems": "100"
    }
    

    I then added --dns-name domain.com to the query and it returned the one result as I expected:

    $ aws route53 list-hosted-zones-by-name --dns-name domain.com
    {
        "HostedZones": [
            {
                "ResourceRecordSetCount": 7, 
                "CallerReference": "12345abcde", 
                "Config": {
                    "Comment": "HostedZone created by Route53 Registrar", 
                    "PrivateZone": false
                }, 
                "Id": "/hostedzone/AABBCCDD", 
                "Name": "domain.com."
            }
        ], 
        "DNSName": "domain.com", 
        "IsTruncated": false, 
        "MaxItems": "100"
    }
    

    You didn't say what language you are using, but the calls are all similar, since it's an API response. If you need help with a specific SDK you should include the language and the code for calling it. But assuming your output is from Javascript, it looks like this is the proper call:

    route53.listHostedZonesByName({ DNSName: 'domain.com' }, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });