The record you cannot write
A zone for a local business's website had been sitting in Route 53 for weeks: 24 record sets, a verbatim mirror of the old host, including the apex A record that still pointed at the old server. That copy was the easy half, and the method is in the post on moving a zone. The hard half is the cutover, the moment the bare domain and its www name start pointing at the CDN that serves the new site.
The www name is simple. It becomes a CNAME to the distribution's
hostname. The apex is not simple. A CNAME cannot share a name with any
other record, and the apex of every zone carries NS and SOA records
by definition. So example.com itself can never be a
CNAME. That is a rule of DNS, not a quirk of one provider.
Route 53's answer is the ALIAS record. It looks like an A record to
the outside world, but instead of addresses it carries an
AliasTarget: the target's hostname and the target's
hosted zone id. Route 53 resolves the target at query time and
answers with the addresses it finds. There is no TTL on the record;
the TTL comes from the target. For CloudFront the target zone id is
one fixed value in every account, which is why a shorthand for it
is worth having.
The importer that could port but not cut over
My infrastructure tool writes zones from a YAML file. I built that
importer in August because the one-record-at-a-time command composed
every name as <name>.<zone> and could not
write an apex record at all, which is where MX, SPF and verification
TXT records live. The importer reads a file, compares each record set
to what the zone holds, and UPSERTs the ones that differ. The
default subcommand is plan, which writes nothing.
apply writes. export dumps the zone back
into the same format so a copy can be diffed against its source.
Every record it knew how to write had a TTL and a list of values. The day before the planned cutover I wrote the cutover file, with the apex as an alias to the distribution, and ran the plan. The loader answered:
cutover.yml: record 0 is missing 'values'
The tool that had ported the zone was a console job at the one step the port existed for. That is my mistake to own. I built the importer for the copy and never thought through the flip, and the gap stayed invisible because the preview of the site had been running on a hostname under my own domain, where the alias was set by a different command on the distribution side.
The file shape
A plain record carries ttl and values. An
alias record carries alias instead, and nothing else.
The three records below are the whole cutover for a site behind
CloudFront: the apex as an alias, the www name as a CNAME, and an
ordinary TXT shown for contrast.
records:
- name: "" # "" or "@" is the zone apex
type: A
alias:
dns_name: d123.cloudfront.net.
hosted_zone_id: cloudfront # shorthand for Z2FDTNDATAQYW2
- name: www
type: CNAME
ttl: 300
values: [d123.cloudfront.net.]
- name: ""
type: TXT
ttl: 3600
values:
- '"v=spf1 include:mail.example.net -all"'
The loader enforces the shape so a hand-written file fails early:
- An alias record with
ttlorvaluesis refused. Route 53 takes the TTL from the target. - An alias must be type A or AAAA. An alias CNAME is refused.
- A record with neither
valuesnoraliasis refused, which is the error above. hosted_zone_idacceptscloudfrontas shorthand for the fixed CloudFront zone, or any real hosted zone id.- NS and SOA are refused outright. Route 53 owns those, and a verbatim port must not carry them over.
What the plan step prints
The plan reads every record set in the zone, including the live
AliasTarget where one exists, and compares it to the
file. The target hostname is lower-cased with a trailing dot and the
zone id shorthand is expanded before the comparison, so a file that
says D123.CloudFront.net matches a zone that says
d123.cloudfront.net.. One line per record, three marks:
= already correct, + new, ~
changed. A changed record gets a second line saying what it was.
With the log prefix stripped, the cutover plan looked like this:
~ example.com. A ALIAS d123.cloudfront.net. (Z2FDTNDATAQYW2)
was: 203.0.113.10
~ www.example.com. CNAME d123.cloudfront.net.
was: example.com.
2 change(s) pending, re-run with 'apply' to write
Two changes is the number I expected, and the was: lines
name exactly what apply would overwrite. The rollback file is the
same two records with the old values, and its plan prints
was: ALIAS d123.cloudfront.net. under the apex. Both
files were dry-run the day before the cutover, two changes each.
With the record TTLs lowered to 300 seconds beforehand, a rollback
reaches resolvers in about five minutes.
Export emits the alias form too, so a dump of a zone that already has one re-imports as a no-op. Eleven unit tests pin the loader rules, the shorthand expansion, the plan output for an alias over a plain record and a plain record over an alias, and the read-back of the live target.
Why the plan step matters. The nameserver flip in
the earlier post is proven by a diff before it happens. The record
flip deserves the same proof. Plan and apply are one code path with
the write at the end, so what the plan shows is what apply does.
The habit is simple: read every was: line, confirm the
count, then apply.
The default that caches a typo for a day
This one has nothing to do with aliases and everything to do with zones you are still adding names to. Route 53 creates a hosted zone with an SOA minimum of 86400 seconds. That field sets how long a resolver may cache a negative answer. Ask for a name one minute before its record exists, or ask for it with a typo, and every resolver that saw the question remembers the name as nonexistent for 24 hours.
I met it in August while bringing my own zone up, when a name I had
just added stayed missing from two machines that had asked a moment
too early. The workaround was to query a public resolver directly and
use curl --resolve until the caches aged out. The fix is
a small subcommand in the same importer that rewrites only the last
field of the SOA record. The import refuses to touch SOA otherwise,
and this is the one legitimate reason to: the record already belongs
to the zone and only its minimum changes.
r53-soa-negative-ttl account=prod zone=example.com ttl=300
negative-cache TTL was 86400s, now 300s
a name queried before it exists is now remembered as nonexistent
for 5 minutes, not 24 hours
Lowering it costs a few more queries against the zone for names that do not exist, which at Route 53's query pricing is nothing. I now treat it as a step in creating any zone that will gain records over time.
If you are about to point a domain at a CDN
- The apex cannot be a CNAME. Use the provider's alias type, and check that your tooling can write it before cutover day.
- An alias carries a target and a target zone id, no TTL. CloudFront's zone id is the same in every account.
- Plan before apply. Read the
was:line for every change and confirm the count. - Write the rollback file at the same time as the cutover file, and dry-run both.
- Lower record TTLs first, so the rollback is minutes rather than hours.
- Set the zone's negative-cache TTL down from the 24-hour default while names are still being added.