

# 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](migrate-app.md).

**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](#migrate-step-9-prepare), [Remove the domain from Amplify Hosting](#migrate-step-9-remove), [Add each hostname to Amplify](#migrate-step-9-add), [Wait for each domain to become active](#migrate-step-9-wait), and [Turn hostnames into redirects](#migrate-step-9-redirects). To keep your other hostnames online during the move, use [Move one hostname at a time](#migrate-step-9-one-at-a-time) in place of the remove and add sections. If something fails, [roll back](#migrate-step-9-rollback). When the site is stable, [decommission the Amplify Hosting app](#migrate-step-10).

## Prepare
<a name="migrate-step-9-prepare"></a>

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

```bash
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.

1. List the hostnames to move, and the Amplify environment for each, based on the branch each one served. For help, see [Before you begin](migrate-app.md#migrate-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.

1. 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.

1. 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
<a name="migrate-step-9-remove"></a>

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.

```bash
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:

  ```bash
  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`.

```bash
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
<a name="migrate-step-9-add"></a>

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.

```bash
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](#migrate-ts-routing-record).

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

## Wait for each domain to become active
<a name="migrate-step-9-wait"></a>

Run this for each hostname:

```bash
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.

```bash
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](#migrate-ts-routing-record).

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

## Turn hostnames into redirects
<a name="migrate-step-9-redirects"></a>

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

```bash
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
<a name="migrate-step-9-rollback"></a>

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

   ```bash
   aws amplifygen3 domain remove www.<your-domain> --yes
   ```

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

1. 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.

   ```bash
   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
   ```

1. 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
<a name="migrate-step-9-one-at-a-time"></a>

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](#migrate-step-9-prepare), and run the command that saves the list of records in [Remove the domain from Amplify Hosting](#migrate-step-9-remove). 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.

   ```bash
   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
   ```

1. 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.

   ```bash
   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.

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

1. Check that the hostname is active, as described in [Wait for each domain to become active](#migrate-step-9-wait).

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](#migrate-step-9-remove) and [Add each hostname to Amplify](#migrate-step-9-add). 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](#migrate-step-9-redirects).

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

## Decommission the Amplify Hosting app
<a name="migrate-step-10"></a>

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:

```bash
# 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:

```bash
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
<a name="migrate-troubleshooting-domain"></a>

### Domain setup fails
<a name="migrate-ts-domain-setup"></a>

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 name="migrate-ts-verifying"></a>

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
<a name="migrate-ts-routing-record"></a>

 `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.

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

   ```bash
   aws amplifygen3 domain remove www.<your-domain> --yes
   ```

1. Add the domain again with `aws amplifygen3 domain add`.