Last active
July 3, 2023 18:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. You saved my life. 👍