Migrate a hosted zone to a different AWS account in few seconds!!
Transferring a hosted zone between AWS accounts might sound like a daunting task, but it doesn't have to be! This guide shows you how to migrate your DNS configurations swiftly and effortlessly. With the right approach, you can replicate your hosted zone's records in a new account without downtime, ensuring seamless domain management. Whether you're restructuring your AWS environment or transferring ownership, these steps will make the process quick and efficient.
Published Nov 26, 2024
Migrating a hosted zone from one AWS account to another involves creating a new hosted zone in the target account, replicating the DNS records, and updating the domain's nameservers. Here's a step-by-step guide for manual and automated steps.
In this manual process, will guide you through migrating a hosted zone using the AWS CLI.
- Install AWS CLI if not already installed.
- Use the following command to export the DNS records from the hosted zone in the source account:
3. Save the output file (
dns-records.json
), which contains all DNS records.- Log in to the target AWS account.
- Navigate to Route 53 and create a new hosted zone with the same domain name.
- Note the new hosted zone ID and nameservers assigned to the zone.
- Use the exported
dns-records.json
to replicate the records. - Transform the JSON file to match the
change-resource-record-sets
API format if needed. An example format looks like this:
3. Import the records to the new hosted zone:
- Go to your domain registrar (e.g., AWS Route 53, GoDaddy, Namecheap).
- Replace the nameservers with the ones provided in the new hosted zone.
- Wait for the DNS propagation, which can take up to 48 hours.
- Use tools like DNS Checker to ensure the records are correctly propagating.
- Confirm that the DNS records are functional and resolving to the expected values.
- Avoid downtime: Keep both hosted zones active until propagation is complete.
- Delegate permissions: If you need cross-account access, consider using AWS Resource Access Manager (RAM) or an IAM role for temporary access.
- Automate the process: Use tools like Terraform or Route 53's APIs for larger migrations.
Here’s a Python script using
boto3
(AWS SDK for Python) to automate the transformation and migration of DNS records between AWS accounts. This script will:- Export DNS records from the source account.
- Transform them into the format required for importing.
- Import the records into the target account.
- Install the required libraries:
2. Set up AWS CLI profiles for both accounts:
- Source account:
aws configure --profile source_account
- Target account:
aws configure --profile target_account
3. Save the python script as migrate_dns.py
- Replace
source-hosted-zone-id
andtarget-hosted-zone-id
with the respective hosted zone IDs. Also replace profiles if you have created differently. - Run the script:
- SOA and NS records are skipped: These are automatically managed by AWS.
- TTL fallback: If a record lacks a TTL, a default value of
300
seconds is applied.