I would like to export a DNS zonefile from my Amazon Route 53 setup. Is this possible, or can zonefiles only be created manually? (e.g. through )
27 Answers
The following script exports some zone details in bind format from Route53. Pass over the domain name as a parameter to script. (This required awscli and jq to be installed and configured.)
#!/bin/bash
zonename=$1
hostedzoneid=$(aws route53 list-hosted-zones --output json | 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"'It should be noted that records that are defined in AWS as pointing to another service will not be exported. You may want to switch to a CNAME for load balancers because their IP addresses can change.
14It's not possible yet. You'll have to use the API's ListResourceRecordSets and build the zonefile yourself.
As stated in the comment, the cli53 is a great tool to interact with Route 53 using the command line interface.
First, configure your account keys in ~/.aws/config file:
[default]
aws_access_key_id = AK.....ZP
aws_secret_access_key = 8j.....M0Then, use the export command:
$ cli53 export --full --debug example.com > example.com.zone 2> example.com.zone.logVerify the example.com.zone file after export to make sure that everything is exported correctly.
You can import the zone lately:
$ cli53 import --file ./example.com.zone example.comAnd if you want to transfer the Route53 zone from one AWS account to another, you can use the profile option. Just add two named accounts to the ~/.aws/config file and reference them with the profile property during export and import. You can even pipe these two commands.
You can export a JSON file:aws route53 list-resource-record-sets --hosted-zone-id <zone-id-here> --output json > route53-records.json
You can export with aws api
aws route53 list-resource-record-sets --hosted-zone-id YOUR_ZONE_ID
Based on @szentmarjay's answer above, except it shows usage and supports zone_id or zone_name. This is my fave because it's standard old school bind format, so other tools can do stuff with it.
#!/bin/bash
# r53_export
usage() { local cmd=$(basename "$0") echo -e >&2 "\nUsage: $cmd {--id ZONE_ID|--domain ZONE_NAME}\n" exit 1
}
while [[ $1 ]]; do if [[ $1 == --id ]]; then shift; zone_id="$1" elif [[ $1 == --domain ]]; then shift; zone_name="$1" else usage fi shift
done
if [[ $zone_name ]]; then zone_id=$( aws route53 list-hosted-zones --output json \ | jq -r ".HostedZones[] | select(.Name == \"$zone_name.\") | .Id" \ | head -n1 \ | cut -d/ -f3 ) echo >&2 "+ Found zone id: '$zone_id'"
fi
[[ $zone_id ]] || usage
aws route53 list-resource-record-sets --hosted-zone-id $zone_id --output json \ | jq -jr '.ResourceRecordSets[] | "\(.Name) \t\(.TTL) \t\(.Type) \t\(.ResourceRecords[]?.Value)\n"' I updated the more close answer from @szentmarjay-tibor and @jacopkane. Thanks to them for working solution
With the following one you should get a right output which you'll be able to write to a file. You will still need jq and configured and authorized aws-cli
#!/bin/bash
if [[ "$1" == "" ]]; then echo "Write a domain name after the script path. Example:" echo " ./script.sh example.com" exit 0
fi
hostedzoneid=$(aws route53 list-hosted-zones --output json | jq -r ".HostedZones[] | select(.Name == \"$1.\") | .Id" | cut -d'/' -f3)
aws route53 list-resource-record-sets --hosted-zone-id $hostedzoneid | \ jq -jr '.ResourceRecordSets[] | "\(.Name) \t\(.TTL) \tIN \t\(.Type) \t\(.ResourceRecords[]?.Value)\n"' | \ sed "s|^$1. |@ |g; s|.$1.||g; s|172800|3600|g" | \ sed 's|\\052|*|g'