Skip to content

Instantly share code, notes, and snippets.

@tibu
Created January 29, 2018 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tibu/8f9c061fa0818ea565e42933ccec42c0 to your computer and use it in GitHub Desktop.
Save tibu/8f9c061fa0818ea565e42933ccec42c0 to your computer and use it in GitHub Desktop.
Download zone file in bind format from AWS Route53
#!/bin/bash
# download zone from AWS Route53
zonename=$1
hostedzoneid=$(aws route53 list-hosted-zones | jq -r ".HostedZones[] | select(.Name == \"$zonename.\") | .Id" | cut -d'/' -f3)
aws route53 list-resource-record-sets --hosted-zone-id $hostedzoneid --output json | jq -jr '.ResourceRecordSets[] | "\(.Name) \t\(.TTL) \t\(.Type) \t\(.ResourceRecords[].Value)\n"'
@georgikoemdzhiev
Copy link

Thank you for the script. It works fine for hosted zones which have only A record types. However, it can be improved to handle the AWS Route 53's proprietary AliasTarget records. For example, the above script would fail with jq: error (at <stdin>:n): Cannot iterate over null (null) exception if we pass this json file into jq:

{
    "ResourceRecordSets": [
        {
            "Name": "domain.com.",
            "Type": "NS",
            "TTL": 172800,
            "ResourceRecords": [
                {
                    "Value": "ns-1536.awsdns-00.co.uk."
                },
                {
                    "Value": "ns-0.awsdns-00.com."
                },
                {
                    "Value": "ns-1024.awsdns-00.org."
                },
                {
                    "Value": "ns-512.awsdns-00.net."
                }
            ]
        },
        {
            "Name": "domain.com.",
            "Type": "SOA",
            "TTL": 900,
            "ResourceRecords": [
                {
                    "Value": "ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
                }
            ]
        },
		        {
            "Name": "test.domain.com.",
            "Type": "A",
            "AliasTarget": {
                "HostedZoneId": "Z1H1FL5HABSF5",
                "DNSName": "internal-load-balancer-111111111.us-west-2.elb.amazonaws.com.",
                "EvaluateTargetHealth": false
            }
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment