Cloud and deployment · Intermediate · 24 min
Point your domain at AWS with Route 53 hosted zones and alias records
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · AWS CLI 2.42.4
You will have a public Route 53 hosted zone answering for your domain or one subdomain, with alias records to CloudFront, CNAME, MX and TXT records written from a change batch file, and a health-checked failover pair for an API.
On this page
A domain name only reaches AWS when the rest of the internet is told to ask Route 53 about it. That takes two separate things: a hosted zone in Route 53 that holds your records, and a delegation at the level above (your registrar, or the zone of a parent domain) that names the hosted zone's four name servers. Many broken setups have one without the other: records carefully written in a zone nobody asks, or name servers switched to an empty zone and email gone for a day.
In this tutorial you do both halves in order, with the AWS CLI and plain JSON files you can keep in version control. You will:
- create a public hosted zone and read the name servers AWS gives it
- delegate either the whole domain, or a single subdomain while the rest of your DNS stays where it is
- write A and AAAA alias records to a CloudFront distribution, plus a CNAME, MX and TXT records, in one change batch
- add an HTTPS health check and a failover pair, so
apimoves to a backup server when the main one stops answering - trace the delegation with
dig +trace, then remove everything
Cost. Route 53 is not in the free tier. On the AWS pricing page (checked 26 November 2026) a hosted zone is 0.50 USD a month for each of your first 25, and a zone deleted within 12 hours of creation is not charged. Standard queries are 0.40 USD per million; queries for alias records that point at CloudFront are free, which is one reason to prefer aliases. A health check on a server outside AWS is 0.75 USD a month, and HTTPS is an optional feature at 2.00 USD a month more, so the check in this tutorial costs 2.75 USD a month while it exists. The last step deletes all of it.
Step 1 · Install the AWS CLI and dig, and set names
Install curl, unzip, jq and dig, then version 2.42.4 of the AWS CLI from Amazon's versioned installer. If aws --version already shows 2.42.4, skip the installer lines; an older v2 is replaced in place:
sudo apt update
sudo apt install -y curl unzip jq ca-certificates bind9-dnsutils
AWS_CLI_VERSION=2.42.4
cd "$(mktemp -d)"
curl -fsSLo awscliv2.zip "https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m)-${AWS_CLI_VERSION}.zip"
unzip -q -o awscliv2.zip
sudo ./aws/install --update
aws --version
dig -v--update lets the installer replace an older AWS CLI v2 and does no harm on a fresh machine. The CLI gets no updates from apt; to upgrade later, run the same commands with a newer version number. The archive is a large download; on prepaid mobile data, fetch it once on Wi-Fi and keep the zip.
Decide what the hosted zone is for. Use the bare domain (example.com) if Route 53 will answer for everything. Use a subdomain (cloud.example.com) if your website, email and other records stay with the DNS provider you have now, and only the AWS parts move. Every later command works from these variables, so replace each value before you run it:
mkdir -p ~/route53-demo
cd ~/route53-demo
export ZONE=example.com # replace: your domain, or a subdomain such as cloud.example.com
export DIST_DOMAIN=d111111abcdef8.cloudfront.net # replace: your distribution's domain name
export HELPDESK_TARGET=acme.helpdesk.example.net # replace: a hostname a provider asked you to CNAME to
echo "Zone: $ZONE"If you use a named profile, run export AWS_PROFILE=<your-profile> as well (replace the placeholder). Route 53 is a global service, so no Region is needed for it. Keep this terminal open: the variables go when it closes.
Check it worked
aws --version starts with aws-cli/2.42.4, dig -v prints a DiG 9. version line, and the echo prints your zone name. aws sts get-caller-identity shows the account you expect; create nothing in the wrong account.
Step 2 · Create the public hosted zone
Creating a zone starts the monthly charge, and running create-hosted-zone twice makes two zones with the same name and different name servers, which is a common source of confusion. The script below looks for a zone it made earlier (tagged with the comment route53-demo) and only creates one if none is found. The caller reference only has to be unique, so a timestamp does.
If Route 53 already hosts this name, do not use it here. A domain registered with Route 53 Domains gets a hosted zone automatically, and that zone holds your live records. This tutorial overwrites apex records and its last step deletes the zone, so the script refuses to touch any zone for $ZONE that it did not create. If it stops with that message, set ZONE to an unused subdomain such as cloud.example.com and run it again:
cd ~/route53-demo
OTHER_ID=$(aws route53 list-hosted-zones-by-name --dns-name "$ZONE" \
--query "HostedZones[?Name=='${ZONE}.' && Config.Comment!='route53-demo'].Id | [0]" --output text)
ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name "$ZONE" \
--query "HostedZones[?Name=='${ZONE}.' && Config.Comment=='route53-demo'].Id | [0]" --output text)
if [ -n "$OTHER_ID" ] && [ "$OTHER_ID" != "None" ]; then
echo "A hosted zone for $ZONE already exists ($OTHER_ID); this tutorial will not change it. Use a subdomain instead."
false
elif [ "$ZONE_ID" = "None" ] || [ -z "$ZONE_ID" ]; then
ZONE_ID=$(aws route53 create-hosted-zone --name "$ZONE" \
--caller-reference "route53-demo-$(date +%s)" \
--hosted-zone-config Comment=route53-demo,PrivateZone=false \
--query HostedZone.Id --output text)
echo "Created $ZONE_ID"
else
echo "Reusing $ZONE_ID (created earlier by this tutorial)"
fi
export ZONE_ID=${ZONE_ID##*/}
echo "$ZONE_ID"The ID comes back as /hostedzone/Z...; the last line keeps only the part after the final slash. Now read the four name servers AWS assigned. They are spread over four top-level domains (.com, .net, .org and .co.uk), so one TLD having trouble does not take your domain down:
aws route53 get-hosted-zone --id "$ZONE_ID" \
--query 'DelegationSet.NameServers[]' --output text | tr '\t' '\n' | tee name-servers.txtEvery new zone also holds two records you must leave alone: the NS record listing those servers and the SOA record that describes the zone:
aws route53 list-resource-record-sets --hosted-zone-id "$ZONE_ID" \
--query 'ResourceRecordSets[].[Name,Type,TTL]' --output tableCheck it worked
name-servers.txt holds four lines that look like ns-1234.awsdns-12.org, and the table shows exactly two rows for your zone name: NS and SOA. Running the first script again prints Reusing with the same ID, not a new zone. If it printed already exists instead, nothing was changed; pick a subdomain for ZONE.
Step 3 · Delegate the domain or the subdomain
Nothing has changed for the outside world yet. Read this step to the end before you change anything at your registrar.
Copy your existing records first. Delegation is a switch: the moment resolvers learn the new name servers, they ask only Route 53. Any record that is not in the hosted zone stops existing, and the usual casualty is email (MX) and domain verification (TXT). Export or write down every record your current provider holds for the name you are moving, and recreate them in the next step before you delegate.
Option A: the whole domain
At the registrar where you bought the domain, find the name server setting (often called "nameservers" or "DNS servers"), choose custom name servers, and enter the four lines from name-servers.txt. Remove the old ones; a mix of old and new servers gives different answers to different people. If your domain is a country-code name such as .ng, .ke or .co.za bought through a local reseller, the setting is in that reseller's panel; some resellers make the change for you through a support request instead.
If you can, lower the TTL on the old provider's records to a few minutes a day ahead. Resolvers that cached an old answer keep it until its TTL runs out; the parent zone's NS records also have their own TTL, often a day or two, which you cannot change. /tutorials/understand-dns-ttls-and-propagation-with-your-own-resolver shows how that caching works.
Option B: one subdomain only
Leave the registrar alone. At the DNS provider that hosts the parent domain now, add one NS record whose name is the subdomain (cloud in a zone for example.com) with the four name servers as its values. AWS suggests 172800 seconds (two days) as the TTL for an NS record. If the parent zone already has records under cloud.example.com, copy them into the new hosted zone and then delete them from the parent; records in both places give inconsistent answers depending on which servers a resolver has cached.
Before or after you delegate, you can ask one of the AWS name servers directly. This works as soon as the zone exists, so it checks the zone without the delegation:
dig @"$(head -n 1 name-servers.txt)" "$ZONE" SOA +norecurseCheck it worked
The direct query shows flags: qr aa (an authoritative answer) and an SOA that names an awsdns server. Once the delegation is in place, follow it from the root of DNS:
dig +trace "$ZONE" NSRead the output from the top: the root servers hand you to the TLD servers, the TLD servers (or, for Option B, your parent domain's servers) hand you to four awsdns servers, and the last block is the NS answer from one of them. If the last referral still lists your old provider, the registrar change has not been made or has not been published yet. dig +short NS "$ZONE" @1.1.1.1 shows what one public resolver currently believes.
Step 4 · Write records with a change batch file
Route 53 takes record changes as a change batch: a JSON document with a list of CREATE, UPSERT or DELETE actions that are applied together or not at all. UPSERT creates the record or replaces it, so the same file can be applied again safely. Keeping the file means you have a reviewed, repeatable copy of your DNS.
This batch holds five record sets:
- an A alias and an AAAA alias at the zone apex pointing at CloudFront. DNS does not allow a CNAME at the apex, and an alias answers with CloudFront's current addresses directly.
Z2FDTNDATAQYW2is the fixed hosted zone ID AWS documents for every CloudFront distribution, andEvaluateTargetHealthmust befalsefor CloudFront. The distribution must list$ZONEas an alternate domain name, and the AAAA record is only useful if IPv6 is on for the distribution (it is by default). - a CNAME for
help, the kind of record a hosted service asks you to add. - an MX record set with two mail servers, each with a priority; lower numbers are tried first. Replace the hostnames with the ones your mail provider gives you.
- a TXT record set with an SPF policy and a site verification string. Each TXT value is wrapped in its own double quotes inside the JSON string, and a single quoted string can be at most 255 characters.
If you have no distribution yet, delete the first two entries in Changes before you apply the file; Route 53 refuses an alias to a distribution that does not exist.
cd ~/route53-demo
cat > records.json <<EOF
{
"Comment": "Website, help desk and mail for $ZONE",
"Changes": [
{"Action": "UPSERT", "ResourceRecordSet": {
"Name": "$ZONE", "Type": "A",
"AliasTarget": {"HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": "$DIST_DOMAIN", "EvaluateTargetHealth": false}}},
{"Action": "UPSERT", "ResourceRecordSet": {
"Name": "$ZONE", "Type": "AAAA",
"AliasTarget": {"HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": "$DIST_DOMAIN", "EvaluateTargetHealth": false}}},
{"Action": "UPSERT", "ResourceRecordSet": {
"Name": "help.$ZONE", "Type": "CNAME", "TTL": 300,
"ResourceRecords": [{"Value": "$HELPDESK_TARGET"}]}},
{"Action": "UPSERT", "ResourceRecordSet": {
"Name": "$ZONE", "Type": "MX", "TTL": 3600,
"ResourceRecords": [{"Value": "10 mx1.mail.example.net."}, {"Value": "20 mx2.mail.example.net."}]}},
{"Action": "UPSERT", "ResourceRecordSet": {
"Name": "$ZONE", "Type": "TXT", "TTL": 3600,
"ResourceRecords": [{"Value": "\"v=spf1 include:_spf.mail.example.net ~all\""}, {"Value": "\"site-verification=replace-with-your-token\""}]}}
]
}
EOF
jq -e '.Changes | length == 5' records.jsonThe EOF is not quoted, so the shell fills in $ZONE and the other variables as it writes the file; jq then proves the result is valid JSON.
Warning: UPSERT replaces any existing record set with the same name and type. Applied to a zone that already serves traffic, this file would replace its apex A, AAAA, MX and TXT records, and the placeholder mail servers would stop your email. Apply it only to the zone the previous step created, after replacing every placeholder with real values. Apply the batch and wait for Route 53 to report it has reached all of its name servers:
CHANGE_ID=$(aws route53 change-resource-record-sets --hosted-zone-id "$ZONE_ID" \
--change-batch file://records.json --query ChangeInfo.Id --output text)
aws route53 wait resource-record-sets-changed --id "$CHANGE_ID"
echo "$CHANGE_ID is in sync"
aws route53 list-resource-record-sets --hosted-zone-id "$ZONE_ID" \
--query 'ResourceRecordSets[].[Name,Type,TTL,AliasTarget.DNSName]' --output tableThe change usually reaches INSYNC within a minute. Alias rows show no TTL in the table: the answer uses CloudFront's own TTL.
Check it worked
The table now lists A and AAAA rows for your zone with your distribution's domain in the last column, a CNAME for help, and MX and TXT rows with their TTLs. You can ask Route 53 what it would answer without waiting for any cache:
aws route53 test-dns-answer --hosted-zone-id "$ZONE_ID" --record-name "$ZONE" --record-type MXThe response has "ResponseCode": "NOERROR" and both MX values. After delegation, dig +short MX "$ZONE" and dig +short TXT "$ZONE" return the same values from any resolver.
Step 5 · Add a health check for the main API server
Failover routing needs to know when a server is down, and a Route 53 health check is how it finds out. Health checkers in several AWS Regions request a URL on your server every 30 seconds (or every 10, at extra cost). For HTTPS, each one must connect within four seconds and get a 2xx or 3xx status within two seconds after that. Route 53 calls the endpoint healthy while more than 18 percent of the checkers report success, so one bad network path does not flip it. HTTPS checks do not validate the certificate.
Set the two server addresses. The values below are documentation addresses that reach nothing; replace them with your main and backup servers' public IPv4 addresses. The main server should answer https://api.<your zone>/health with a 200 when the app is working:
export PRIMARY_IP=203.0.113.10 # replace: the main API server
export BACKUP_IP=198.51.100.20 # replace: the backup API serverWrite the check as a file and create it. FullyQualifiedDomainName with EnableSNI makes the checkers send the right hostname, so a server with several sites returns the right certificate and app. FailureThreshold is the number of checks in a row that must fail before the status changes:
cd ~/route53-demo
cat > health-check.json <<EOF
{
"IPAddress": "$PRIMARY_IP",
"Port": 443,
"Type": "HTTPS",
"ResourcePath": "/health",
"FullyQualifiedDomainName": "api.$ZONE",
"EnableSNI": true,
"RequestInterval": 30,
"FailureThreshold": 3
}
EOF
export HC_ID=$(aws route53 create-health-check \
--caller-reference "api-primary-$(date +%s)" \
--health-check-config file://health-check.json \
--query HealthCheck.Id --output text)
aws route53 change-tags-for-resource --resource-type healthcheck \
--resource-id "$HC_ID" --add-tags Key=Name,Value=api-primary
echo "$HC_ID"The Name tag is what the console shows in its list of health checks; without it you see only an ID.
If the server has a firewall, it must let the checkers in. AWS publishes their address ranges in ip-ranges.json under the service name ROUTE53_HEALTHCHECKS:
curl -fsS https://ip-ranges.amazonaws.com/ip-ranges.json \
| jq -r '.prefixes[] | select(.service == "ROUTE53_HEALTHCHECKS") | .ip_prefix' | sort -u | tee healthcheck-ranges.txt | head -n 5Check it worked
echo "$HC_ID" prints an ID like a1b2c3d4-.... A few minutes after creation, ask what the checkers see:
aws route53 get-health-check-status --health-check-id "$HC_ID" \
--query 'HealthCheckObservations[].[Region,StatusReport.Status]' --output textEach line names a checker Region and a status such as Success: HTTP Status Code 200. Lines with Failure and a timeout usually mean the firewall is blocking the ranges in healthcheck-ranges.txt, or nothing is listening on port 443.
Step 6 · Route api to a backup server with failover records
A failover pair is two record sets with the same name and type. The PRIMARY one is returned while its health check is healthy; when it is not, Route 53 answers with the SECONDARY one instead. Each needs a SetIdentifier to tell them apart. A short TTL of 60 seconds limits how long resolvers keep handing out the dead address after a switch, at the price of more queries:
cd ~/route53-demo
cat > failover.json <<EOF
{
"Comment": "Failover pair for api.$ZONE",
"Changes": [
{"Action": "UPSERT", "ResourceRecordSet": {
"Name": "api.$ZONE", "Type": "A", "TTL": 60,
"SetIdentifier": "api-primary", "Failover": "PRIMARY",
"HealthCheckId": "$HC_ID",
"ResourceRecords": [{"Value": "$PRIMARY_IP"}]}},
{"Action": "UPSERT", "ResourceRecordSet": {
"Name": "api.$ZONE", "Type": "A", "TTL": 60,
"SetIdentifier": "api-backup", "Failover": "SECONDARY",
"ResourceRecords": [{"Value": "$BACKUP_IP"}]}}
]
}
EOF
CHANGE_ID=$(aws route53 change-resource-record-sets --hosted-zone-id "$ZONE_ID" \
--change-batch file://failover.json --query ChangeInfo.Id --output text)
aws route53 wait resource-record-sets-changed --id "$CHANGE_ID"
aws route53 list-resource-record-sets --hosted-zone-id "$ZONE_ID" \
--query "ResourceRecordSets[?Name=='api.${ZONE}.'].[SetIdentifier,Failover,HealthCheckId,ResourceRecords[0].Value]" \
--output tableThe secondary has no health check here, so Route 53 treats it as always healthy and it is the last resort. Give it its own check if you would rather return nothing than a backup that is also down. Users on mobile networks often sit behind resolvers that hold answers a little longer than the TTL, so expect a short tail of requests to the old address after any switch.
Check it worked
The table has two rows: api-primary with PRIMARY, your health check ID and the main address, and api-backup with SECONDARY and the backup address. With the delegation in place, ask one of your zone's name servers directly so no cache is involved:
dig +short api."$ZONE" @"$(head -n 1 name-servers.txt)"It prints the main address. To see the failover, stop the app on the main server (or make /health return a 500), wait about two minutes (three failed checks 30 seconds apart, plus the time for the status to spread), and run the dig again: it prints the backup address. Start the app again and the answer returns to the main address once the check is healthy.
Step 7 · Clean up
Undo the delegation first. If you pointed a real domain at this zone, deleting it makes the domain (or the subdomain) stop resolving everywhere. For Option A, put the old name servers back at the registrar, or move to a zone you are keeping; for Option B, delete the NS record for the subdomain at the parent provider. Then wait for the NS TTL to pass before you delete the zone.
A hosted zone can only be deleted when it holds nothing but its own NS and SOA records. The script below stops unless ZONE_ID is set, lists every other record set, turns the list into a DELETE batch (Route 53 needs each record's exact values to delete it, which the listing provides) and applies it. Check that ZONE is the demo zone before you run it:
cd ~/route53-demo
if [ -z "${ZONE_ID:-}" ]; then echo "ZONE_ID is not set, stopping"; false; fi
aws route53 list-resource-record-sets --hosted-zone-id "$ZONE_ID" --output json \
| jq --arg apex "${ZONE}." '{Changes: [.ResourceRecordSets[]
| select((.Type == "NS" or .Type == "SOA") and .Name == $apex | not)
| {Action: "DELETE", ResourceRecordSet: .}]}' > delete.json
jq -r '.Changes[].ResourceRecordSet | "\(.Name) \(.Type) \(.SetIdentifier // "")"' delete.json
CHANGE_ID=$(aws route53 change-resource-record-sets --hosted-zone-id "$ZONE_ID" \
--change-batch file://delete.json --query ChangeInfo.Id --output text)
aws route53 wait resource-record-sets-changed --id "$CHANGE_ID"Then delete the health check and the zone, and the local files:
aws route53 delete-health-check --health-check-id "$HC_ID"
aws route53 delete-hosted-zone --id "$ZONE_ID"
cd ~
rm -rf ~/route53-demoCheck it worked
aws route53 get-hosted-zone --id "$ZONE_ID" fails with NoSuchHostedZone, aws route53 list-health-checks --query 'HealthChecks[].Id' no longer contains your health check ID, and ~/route53-demo is gone. The next bill shows the hosted zone charge for this month only (or none, if you deleted it within 12 hours) and a prorated amount for the health check.
Next steps
- Read each record type you just wrote with
dig, and see what the answers mean: DNS records explained. - See how resolvers cache answers and why a change takes time to reach everyone: understand DNS TTLs and propagation with your own resolver.
- Build the CloudFront distribution the alias records point at: host a static site on S3 and CloudFront.
- Keep that distribution fast and fresh after each deploy: cache and invalidate content on CloudFront.
Related tutorials
- Tag cloud resources so you know what costs whatYou will have a four-key tagging scheme applied to every AWS resource through Terraform default_tags, a script that lists resources missing a tag, a tag policy ready for your organisation and cost allocation tags that split the bill by project.
- Store and serve files with Cloudflare R2 from a WorkerYou will build a Worker that uploads, downloads and lists files in an R2 bucket, with a size limit, a content-type allowlist, ETag and range support and a bearer token, test it locally with Wrangler and Vitest, and reach the same bucket through R2's S3-compatible API.
- Store container images in Amazon ECR with scanning and lifecycle policiesYou will have a private Amazon ECR repository with immutable tags and scan on push, an nginx image copied into it with crane, its vulnerability findings read from the CLI, a lifecycle policy that keeps the ten newest tagged images, and pull access granted to a second AWS account.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials