Skip to content

Instantly share code, notes, and snippets.

@ajsharp
Last active July 3, 2023 18:35
Show Gist options
  • Save ajsharp/e8e1401aedd98bbf0271 to your computer and use it in GitHub Desktop.
Save ajsharp/e8e1401aedd98bbf0271 to your computer and use it in GitHub Desktop.
This script makes it very easy to migrate an entire DNS zone from one AWS Route53 zone to another. This script expects you to have downloaded the old zone file via the AWS CLI to a json file called DOMAIN.zone.json. It uses the AWS CLI to perform the request, so make sure that your credentials are properly configured.
#!/usr/bin/env ruby
require 'json'
# This is the json output of the old zone, fetched using the AWS CLI.
zone = JSON.parse(File.read('DOMAIN.zone.json'))['ResourceRecordSets']
new_zone_id = 'NEW_ZONE_ID'
# We don't want to migrate the SOA and NS records from the old zone.
# The new zone has new values for these and we want to keep them.
zone.reject! { |set| ['SOA', 'NS'].include?(set['Type']) }
result = {
'HostedZoneId' => new_zone_id,
'ChangeBatch' => {
'Changes' => [],
}
}
zone.each do |set|
result['ChangeBatch']['Changes'] << {
'Action' => "CREATE",
'ResourceRecordSet' => set
}
end
json = JSON.generate(result)
exec "aws route53 change-resource-record-sets --hosted-zone-id #{new_zone_id} --cli-input-json '#{json}' --profile YOUR_PROFILE"
@pstanton237
Copy link

Thank you. You saved my life. 👍

@ahmgithubahm
Copy link

cheers, very useful

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