Sub topic 6
Alias records and the zone apex
This chapter assumes you can create a public hosted zone, add basic records, and read a change batch (a JSON file that lists record changes). It covers when to use an alias record instead of a CNAME, and why the zone apex makes the difference matter.
The zone apex problem
The zone apex is the bare domain name, such as example.com, with no www or other prefix. Every zone must have an SOA record and NS records at its apex. The DNS standard says a CNAME record cannot exist alongside any other record for the same name. So you cannot put a CNAME at example.com, and Route 53 rejects the change.
This is a problem when your site runs on a service that gives you a hostname instead of a fixed IP address. CloudFront, load balancers and similar services all work this way. You want example.com to reach them, but a CNAME is not allowed there.
What an alias record is
An alias record is a Route 53 extension to DNS. It looks like an A or AAAA record to the client, but Route 53 fills in the IP addresses at query time by looking up the AWS resource you named. The resolver never sees a CNAME, so the apex rule is not broken.
Alias records differ from CNAME records in these ways:
| Feature | Alias | CNAME |
|---|---|---|
| Allowed at the zone apex | Yes | No |
| Query charges for AWS targets | Free | Charged as standard queries |
| TTL | Taken from the target; you cannot set it | You set it |
| Can point to | Specific AWS resources, or another record in the same zone | Any DNS name |
| Record type in the response | A or AAAA | CNAME |
Common alias targets are CloudFront distributions, Application and Network Load Balancers, S3 static website endpoints, API Gateway custom domains, Elastic Beanstalk environments, Global Accelerator, and other records in the same hosted zone. An EC2 instance's public DNS name is not a valid target. Use an A record with its Elastic IP instead.
Creating an alias at the apex
The change batch below points the apex at a CloudFront distribution. Note that there is no TTL and no ResourceRecords. Instead, an AliasTarget holds three fields.
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "example.com",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "Z2FDTNDATAQYW2",
"DNSName": "d111111abcdef8.cloudfront.net",
"EvaluateTargetHealth": false
}
}
}
]
}DNSNameis the name of the target resource.HostedZoneIdis the hosted zone ID of the target, not your own zone's ID. For CloudFront it is alwaysZ2FDTNDATAQYW2. For a load balancer it depends on the region and the load balancer type. Read it from the load balancer's description (CanonicalHostedZoneIdinaws elbv2 describe-load-balancers).EvaluateTargetHealthtells Route 53 whether to treat the target's own health as the record's health. This matters for routing policies, which a later chapter covers.
Apply the file with the command you already know:
aws route53 change-resource-record-sets \
--hosted-zone-id ZONE_ID \
--change-batch file://apex-alias.jsonIf the target supports IPv6, which CloudFront does when enabled, add a second record with "Type": "AAAA" and the same AliasTarget. Otherwise IPv6 clients cannot use the alias.
Common errors
- Wrong type. An alias must use the record type of its target. Use A or AAAA for CloudFront and load balancers.
- Wrong hosted zone ID. Using your own zone's ID in
AliasTargetfails with an invalid input error. Use the target's ID. - Trying to set a TTL. Route 53 rejects a
TTLfield on an alias record. Remove it. - Existing record with the same name and type.
CREATEfails if one exists. UseUPSERTto replace it. - Target not configured for your name. For CloudFront, the distribution must list
example.comas an alternate domain name and have a matching certificate. Otherwise the DNS answer is correct but HTTPS requests fail. - Expecting alias to work outside Route 53. The alias exists only in Route 53 zones. If you delegate the domain elsewhere, it will not resolve.
Exercise
A teammate tries to create a CNAME record for example.com that points to a load balancer's DNS name, and Route 53 rejects it. What should they create instead, and which hosted zone ID goes in AliasTarget?
Show answer
They should create an alias record of type A (and AAAA if the load balancer supports IPv6) at example.com. The HostedZoneId in AliasTarget is the load balancer's own canonical hosted zone ID for its region, not the ID of their example.com zone.