View a markdown version of this page

Transfer your custom domain from Amplify Hosting - AWS Amplify

Transfer your custom domain from Amplify Hosting

This is the only step of the migration that causes downtime. Do it after you deploy and test your app on its Amplify URL, as described in Steps to migrate your app.

Important

Your site is unavailable on the custom domain during the transfer. A domain can be connected to only one app at a time, so you remove it from Amplify Hosting before you add it to Amplify. The outage starts when you remove the domain from Amplify Hosting. It lasts until Amplify verifies the domain and DNS propagates. Expect 1.5 to 3 minutes per hostname when you delete the old records first, and sometimes more than 15 minutes. Schedule the transfer for a low-traffic period. If your site can’t go offline at all, contact AWS Support before you start.

Do the sections in order: Prepare, Remove the domain from Amplify Hosting, Add each hostname to Amplify, Wait for each domain to become active, and Turn hostnames into redirects. To keep your other hostnames online during the move, use Move one hostname at a time in place of the remove and add sections. If something fails, roll back. When the site is stable, decommission the Amplify Hosting app.

Prepare

The commands on this page use your Amplify Hosting app ID. If you opened a new terminal, set it again:

export AMPLIFY_HOSTING_APP_ID=<your-amplify-hosting-app-id>
  1. A day ahead, lower two time to live (TTL) values to 60 seconds. Write down the original values and restore them after the transfer.

    • Each subdomain’s canonical name (CNAME) record. The root domain’s A (alias) record has no TTL, so skip it.

    • Your hosted zone’s SOA record. During the transfer, each hostname briefly doesn’t exist in DNS. Resolvers remember that answer for the SOA record’s TTL, which is 900 seconds by default in Route 53. A visitor who looks up your site at that moment can stay offline for up to 15 minutes after Amplify serves it. In the Route 53 console, choose your hosted zone, select the SOA record, and choose Edit record. Set TTL to 60, change the last number of the value to 60, and choose Save.

    • With another DNS provider, lower the TTL of the records that point at Amplify Hosting. Check how long your provider caches a "does not exist" answer.

  2. List the hostnames to move, and the Amplify environment for each, based on the branch each one served. For help, see Before you begin in Steps to migrate your app. Amplify Hosting groups subdomains under one root domain. In Amplify, each hostname is its own domain.

  3. Check that each target environment has a successful deployment of the version you tested. You can connect a domain only to an environment that has deployed.

  4. If your domain has CAA records, make sure they allow amazon.com. Otherwise Amplify can’t issue the certificate.

Done when: the TTLs are lowered, the hostname list is final, and each target environment has a deployment.

Remove the domain from Amplify Hosting

Amplify Hosting creates a routing record for the root domain and one for each subdomain. Amplify can’t add a hostname while its old record exists. First, save the list of these records. Run this before you remove the domain, because it reads the domain from your Amplify Hosting app. Replace <your-domain> with your root domain and <zone-id> with your Route 53 hosted zone ID.

HOSTING_CF_DOMAIN=$(aws amplify get-domain-association --app-id $AMPLIFY_HOSTING_APP_ID --domain-name <your-domain> \ --query 'domainAssociation.subDomains[0].dnsRecord' --output text | awk '{print $NF}') if [ -z "$HOSTING_CF_DOMAIN" ]; then echo "Domain not found in Amplify Hosting. Check the domain name; run this before you remove the domain." else aws route53 list-resource-record-sets --hosted-zone-id <zone-id> \ --query "ResourceRecordSets[?AliasTarget.DNSName=='$HOSTING_CF_DOMAIN.' || ResourceRecords[0].Value=='$HOSTING_CF_DOMAIN']" > hosting-records.json fi

hosting-records.json lists the root domain’s A (alias) record, if you connected the root domain, and one CNAME record for each subdomain. It doesn’t list the certificate validation record. Amplify Hosting leaves that record behind, and you don’t need to delete it.

With another DNS provider, $HOSTING_CF_DOMAIN is the CloudFront domain name (for example, d111111abcdef8.cloudfront.net). The records to delete are the ones whose value is that name.

Remove the domain from Amplify Hosting. This removes the root domain and all its subdomains.

  • Console: choose your app, then Hosting, Custom domains. On the domain, choose Actions, Delete domain, type delete, and choose Delete domain.

  • CLI:

    aws amplify delete-domain-association --app-id $AMPLIFY_HOSTING_APP_ID --domain-name <your-domain>

Delete the remaining records right away. Amplify Hosting doesn’t always delete its own records. It might already have removed some, so list them again instead of using hosting-records.json, and delete them in one change. Run this in the same shell as the first command, because it uses $HOSTING_CF_DOMAIN.

aws route53 list-resource-record-sets --hosted-zone-id <zone-id> \ --query "{Changes: ResourceRecordSets[?AliasTarget.DNSName=='$HOSTING_CF_DOMAIN.' || ResourceRecords[0].Value=='$HOSTING_CF_DOMAIN'].{Action: 'DELETE', ResourceRecordSet: @}}" > delete-hosting-records.json if grep -q ResourceRecordSet delete-hosting-records.json; then aws route53 change-resource-record-sets --hosted-zone-id <zone-id> --change-batch file://delete-hosting-records.json else echo "Amplify Hosting already removed its records." fi

If change-resource-record-sets fails with InvalidChangeBatch because a record wasn’t found, Amplify Hosting removed it in the meantime. Run both commands again. With another DNS provider, delete at your provider the records whose value is the CloudFront domain name.

Done when: no record in your DNS zone points at the Amplify Hosting CloudFront domain name.

Add each hostname to Amplify

Add every hostname right after you delete the records, including the hostnames that will redirect. Connect production hostnames, and hostnames that will redirect, to production. You turn on redirects later. Use your own hostnames and environments.

aws amplifygen3 domain add www.<your-domain> --env production aws amplifygen3 domain add <your-domain> --env production aws amplifygen3 domain add dev.<your-domain> --env staging

In the console, choose your app, then Settings. For Domains, choose Add domain. Enter the hostname, connect it to the environment, and choose Add domain. Repeat for each hostname.

DNS What you do

Route 53 hosted zone in the same AWS account

Nothing more. Amplify creates the new records. You deleted the old ones in the previous section.

Another DNS provider (Cloudflare, GoDaddy, and others)

Add the records that Amplify prints. Replace the record that pointed at Amplify Hosting with the new routing record. For a root domain, use your provider’s ALIAS or CNAME flattening.

To see the records again, run aws amplifygen3 domain get www.<your-domain>, or open the domain under Settings in the console.

If a hostname won’t add because its old routing record still exists, see Your domain already has a routing record.

Done when: domain add accepts every hostname and its DNS records exist.

Wait for each domain to become active

Run this for each hostname:

aws amplifygen3 domain verify www.<your-domain> --watch --timeout 30m

The command exits with code 0 when the domain is active. If it exits with another code, run aws amplifygen3 domain get www.<your-domain> to see the reason.

Keep checking the site on the custom domain for about 5 minutes after the domain is active, because it can stop resolving for a short time. Visitors whose DNS still points at Amplify Hosting reach Amplify anyway, because CloudFront routes by hostname.

About a minute after a hostname becomes active, check what a public DNS resolver returns for it. Replace <resolver-ip> with the IP address of a public DNS resolver.

dig +short @<resolver-ip> www.<your-domain>

On Windows, use nslookup www.<your-domain> <resolver-ip>. The first answer must match the routing record that domain get lists, for example <app-name>.amplify.app. for a subdomain or an IP address for the root domain. If it shows the record that pointed at Amplify Hosting, delete that record. See Your domain already has a routing record.

Done when: domain verify exits with code 0 for every hostname and the resolver returns the Amplify record.

Turn hostnames into redirects

When the target is active, change each hostname that should redirect:

aws amplifygen3 domain edit <your-domain> --redirect-to www.<your-domain>

The redirect uses status code 301 (permanent) by default. To use another code, pass --status-code with 302, 307, or 308. In the console, choose your app, Settings, then the domain, and set it to redirect to the target.

Done when: the hostname redirects to the target.

If you need to roll back

  1. Remove each hostname you moved from Amplify. Removing a domain also removes the domains that redirect to it.

    aws amplifygen3 domain remove www.<your-domain> --yes
  2. Wait about 2 minutes, until aws amplifygen3 domain get www.<your-domain> reports that the domain doesn’t exist.

  3. Add the domain back to the Amplify Hosting app. In the Amplify Hosting console, choose Hosting, Custom domains, Add domain. Or use the CLI. Restore every hostname you removed, not only www. After --sub-domain-settings, list one prefix=<prefix>,branchName=<branch> entry for each hostname. An empty prefix= is the root domain. Replace main with the branch each hostname served.

    aws amplify create-domain-association --app-id $AMPLIFY_HOSTING_APP_ID --domain-name <your-domain> \ --sub-domain-settings prefix=,branchName=main prefix=www,branchName=main
  4. With another DNS provider, restore the records that pointed at Amplify Hosting.

The site is offline for 5 to 7 minutes during a rollback. Keep the Amplify Hosting app until you are confident in the transfer.

When you remove a domain that uses Route 53, Amplify deletes only its routing record. It keeps the certificate validation CNAME record (the name starts with an underscore and the value ends in acm-validations.aws) and the ownership TXT record (_amplify-challenge.www.<your-domain>), so adding the domain again is faster. They don’t affect Amplify Hosting. If you won’t move the domain to Amplify again, you can delete both.

Done when: the site serves from Amplify Hosting on every hostname you restored.

Move one hostname at a time

The earlier sections take every hostname offline together. To keep the other hostnames online, remove one subdomain at a time from the Amplify Hosting domain, and move the root domain last. In testing, hostnames still on Amplify Hosting had no failed requests, and each moved hostname was offline for about 1.5 minutes. The whole transfer takes longer, because you move the hostnames one after another.

Before you start, finish Prepare, and run the command that saves the list of records in Remove the domain from Amplify Hosting. Run all of the following commands in the same shell, because they use $HOSTING_CF_DOMAIN.

For each subdomain, for example www:

  1. Remove the subdomain from the Amplify Hosting domain, and keep the others. Amplify Hosting stops serving the hostname when the update starts.

    SUB=www aws amplify get-domain-association --app-id $AMPLIFY_HOSTING_APP_ID --domain-name <your-domain> \ --query "domainAssociation.subDomains[?subDomainSetting.prefix!='$SUB'].subDomainSetting" > keep.json aws amplify update-domain-association --app-id $AMPLIFY_HOSTING_APP_ID --domain-name <your-domain> \ --sub-domain-settings file://keep.json
  2. Delete the hostname’s record, and add the hostname to Amplify right away. Don’t wait for the Amplify Hosting update to finish. Amplify accepts the hostname during the update. In testing, waiting tripled the outage, from 1.5 minutes to 4.5 minutes. Replace <environment> with the environment the hostname serves.

    aws route53 list-resource-record-sets --hosted-zone-id <zone-id> \ --query "{Changes: ResourceRecordSets[?Name=='$SUB.<your-domain>.' && ResourceRecords[0].Value=='$HOSTING_CF_DOMAIN'].{Action: 'DELETE', ResourceRecordSet: @}}" > delete-record.json if grep -q ResourceRecordSet delete-record.json; then aws route53 change-resource-record-sets --hosted-zone-id <zone-id> --change-batch file://delete-record.json else echo "Amplify Hosting already removed the record." fi aws amplifygen3 domain add $SUB.<your-domain> --env <environment>

    With another DNS provider, delete the hostname’s record at your provider, and add the records that Amplify prints.

  3. Lower the TTL of the remaining subdomains' CNAME records to 60 seconds again, because the update resets them to 500 seconds.

  4. Check that the hostname is active, as described in Wait for each domain to become active.

When only the root domain remains, remove it with delete-domain-association, delete its record, and add it to Amplify. Follow Remove the domain from Amplify Hosting and Add each hostname to Amplify. If you didn’t connect the root domain, do this for the last subdomain instead, because update-domain-association needs at least one subdomain. Then continue with Turn hostnames into redirects.

Done when: every hostname is active in Amplify and none is left in the Amplify Hosting domain.

Decommission the Amplify Hosting app

Keep the Amplify Hosting app for a few days after the transfer, in case you need to roll back. Stop it from building, so that pushes don’t deploy to both apps:

# Stop builds on each connected branch, and pull request previews aws amplify update-branch --app-id $AMPLIFY_HOSTING_APP_ID --branch-name main \ --no-enable-auto-build --no-enable-pull-request-preview # Stop new branches from being connected automatically aws amplify update-app --app-id $AMPLIFY_HOSTING_APP_ID --no-enable-auto-branch-creation

Repeat update-branch for each connected branch. In the console, choose App settings, Branch settings, select the branch, and choose Actions, Disable auto build.

When you no longer need the app, delete it:

aws amplify delete-app --app-id $AMPLIFY_HOSTING_APP_ID

In the console, choose App settings, General settings, Delete app, type delete, and choose Delete app.

Done when: the Amplify Hosting app no longer builds, or no longer exists after you delete it.

Troubleshooting

Domain setup fails

The hostname is probably still connected to your Amplify Hosting app or to another AWS service. Remove it there. You can only remove a domain whose setup failed, so run aws amplifygen3 domain remove <domain> --yes, then add it again.

The domain stays in verifying

A DNS record is missing or wrong, or CAA records block the certificate. Run aws amplifygen3 domain get <domain>, and check that each record it lists exists at your DNS provider with the exact name and value. Check that the routing record no longer points at Amplify Hosting, and that CAA records allow amazon.com.

Your domain already has a routing record

aws amplifygen3 domain verify or the console shows this error when the hostname still has a DNS record in your Route 53 hosted zone, usually the record that pointed at Amplify Hosting. Amplify doesn’t replace a record that it didn’t create. To fix it:

  1. In your hosted zone, delete the record for the hostname: the CNAME record, or for the root domain the A or alias record. Pointing the record at Amplify instead doesn’t work.

  2. Remove the failed domain from Amplify, and wait about 40 seconds until it no longer appears in aws amplifygen3 domain list.

    aws amplifygen3 domain remove www.<your-domain> --yes
  3. Add the domain again with aws amplifygen3 domain add.