Backing Up Route53 Records
In this post, we’ll see how can we take a dump of Route53 records. This can be useful in migration scenarios where you need to change DNS entries for a lot of URLs and you want to take a backup of original entries.
Pre-requisite: You need to have a Route53 zone with some DNS entries.
Step 1: Create a policy with the following IAM permission.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "route53:ListResourceRecordSets",
"Resource": "*"
}
]
}
Step 2: Create an IAM user with programmatic access and attach the policy created earlier to it. You need to set up AWS CLI using this user’s credentials.
Step 3: Copy the following code and save it on your local system. Specify the zone id of your Route53 hosted zone which you need to take backup of.
import boto3
zone_id=''
client = boto3.client('route53')
paginator = client.get_paginator('list_resource_record_sets')
try:
source_zone_records = paginator.paginate(HostedZoneId=zone_id)
for record_set in source_zone_records:
for record in record_set['ResourceRecordSets']:
if record['Type'] in ['A','CNAME']:
if 'AliasTarget' in record:
print (record['Name']+','+record['Type']+','+record['AliasTarget']['DNSName'])
else:
records=[]
for ip in record['ResourceRecords']:
records.append(ip['Value'])
print (record['Name']+','+record['Type']+','+','.join(records))
except Exception as error:
print(record)
print ('An error occured getting source zone records '+ str(error))
exit(1)
Step 4: Execute the script and save the output to a text file.
That’s all. Now you can make changes to your DNS records and revert them to original values in case something goes wrong.