Exporting DNS zonefile from Amazon Route 53

Amazon Web-ServicesDnsAmazon Route53

Amazon Web-Services Problem Overview


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 http://www.zonefile.org/?lang=en)

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

The following script exports 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"'

Solution 2 - Amazon Web-Services

It's not possible yet. You'll have to use the API's ListResourceRecordSets and build the zonefile yourself.

Solution 3 - Amazon Web-Services

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.....M0

Then, use the export command:

$ cli53 export --full --debug example.com > example.com.zone 2> example.com.zone.log

Verify 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.com

And 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.

Solution 4 - Amazon Web-Services

You can export with aws api

aws route53 list-resource-record-sets --hosted-zone-id YOUR_ZONE_ID

Solution 5 - Amazon Web-Services

Exporting and importing is possible with https://github.com/RisingOak/route53-transfer

Solution 6 - Amazon Web-Services

You can export a JSON file: aws route53 list-resource-record-sets --hosted-zone-id <zone-id-here> --output json > route53-records.json

Solution 7 - Amazon Web-Services

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"'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncasparjespersenView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesSzentmarjay TiborView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesIoan Alexandru CucuView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesSlava Fomin IIView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesYo ImView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesAndré VeelkenView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesBroshiView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesliloleView Answer on Stackoverflow