Sub topic 9
Private hosted zones for VPC-internal names
This chapter assumes you can create a public hosted zone and edit records with change batches, as covered earlier in this tutorial. It also assumes you know what a VPC (Virtual Private Cloud) is and can read its CIDR range. Here you will create a private hosted zone, attach it to VPCs, and learn why names sometimes fail to resolve.
What a private hosted zone is
A private hosted zone (PHZ) is a container for DNS records that Route 53 answers only for VPCs you have associated with it. The records look like those in a public zone, and you manage them with the same API and the same change batches. The difference is who can see them. Nobody on the internet can query a private zone, because its name servers are not reachable from outside your associated VPCs.
Typical uses:
- Giving internal services stable names such as
db.internal.example.cominstead of raw IP addresses or auto-generated names likeip-10-0-1-25.ec2.internal. - Hiding internal topology from the internet.
- Returning a different answer for a public name when it is queried from inside the VPC (split-horizon DNS, covered below).
How resolution works inside a VPC
Every VPC has a built-in DNS resolver, the Route 53 Resolver. It listens at the VPC's base network address plus two (for 10.0.0.0/16, that is 10.0.0.2) and at the link-local address 169.254.169.253. EC2 instances normally use it through DHCP without any configuration.
When an instance asks for a name, the Resolver checks the private hosted zones associated with that VPC first. If a zone matches the name, the Resolver answers from that zone only. If no private zone matches, it resolves the name through public DNS as usual.
Two VPC attributes must be enabled for private zones to work:
enableDnsSupportturns on the Resolver itself.enableDnsHostnamesis required for private hosted zones to resolve.
The next diagram shows the decision the Resolver makes for each query.
Creating a private hosted zone
Creating a private zone is the same call as for a public zone, with a --vpc argument. That argument turns the zone private and associates the first VPC.
aws route53 create-hosted-zone \
--name internal.example.com \
--caller-reference "$(date +%s)" \
--vpc VPCRegion=us-east-1,VPCId=vpc-0123456789abcdef0Before you run this, confirm both VPC attributes are on:
aws ec2 describe-vpc-attribute \
--vpc-id vpc-0123456789abcdef0 \
--attribute enableDnsSupport
aws ec2 describe-vpc-attribute \
--vpc-id vpc-0123456789abcdef0 \
--attribute enableDnsHostnamesIf either value is false, enable it:
aws ec2 modify-vpc-attribute \
--vpc-id vpc-0123456789abcdef0 \
--enable-dns-hostnames "{\"Value\":true}"The zone is not delegated from anywhere. You do not change name servers at a registrar, and the NS record Route 53 creates in a private zone has no effect on resolution. The zone name also does not have to be a domain you own. Using a subdomain of a domain you own, such as internal.example.com, is the safe convention, because it avoids collisions with names that belong to someone else.
Adding records
Records in a private zone are created with the same change batch format you used for public zones. This batch creates an A record for a database host and a CNAME for a friendly service name:
{
"Comment": "Internal names for the orders service",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "db.internal.example.com",
"Type": "A",
"TTL": 60,
"ResourceRecords": [{ "Value": "10.0.1.25" }]
}
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "orders-db.internal.example.com",
"Type": "CNAME",
"TTL": 60,
"ResourceRecords": [{ "Value": "db.internal.example.com" }]
}
}
]
}Save it as internal-records.json and apply it:
aws route53 change-resource-record-sets \
--hosted-zone-id PRIVATE_ZONE_ID \
--change-batch file://internal-records.jsonAlias records work in private zones too. They can point at supported AWS targets such as a load balancer, or at another record in the same zone. Check the current list of supported alias targets for private zones before you rely on one, because it differs from the public zone list.
Associating more VPCs
A private zone can serve many VPCs, and a VPC can be associated with many private zones. Associate an additional VPC in the same account like this:
aws route53 associate-vpc-with-hosted-zone \
--hosted-zone-id PRIVATE_ZONE_ID \
--vpc VPCRegion=eu-west-1,VPCId=vpc-0fedcba9876543210Associations are not inherited through networking. Peering two VPCs, or attaching them to a transit gateway, lets their traffic flow but does not share private zones. Each VPC that needs the names must be associated explicitly.
Associating a VPC from another account
When the VPC belongs to a different account than the zone, the two sides must agree. The zone owner authorizes the association, then the VPC owner completes it:
- In the zone owner's account, authorize the VPC:
aws route53 create-vpc-association-authorization \
--hosted-zone-id PRIVATE_ZONE_ID \
--vpc VPCRegion=us-east-1,VPCId=vpc-0aaaabbbbccccdddd- In the VPC owner's account, create the association:
aws route53 associate-vpc-with-hosted-zone \
--hosted-zone-id PRIVATE_ZONE_ID \
--vpc VPCRegion=us-east-1,VPCId=vpc-0aaaabbbbccccdddd- In the zone owner's account, delete the authorization. The association stays in place, and removing the authorization prevents the same VPC from being re-associated later without a new approval:
aws route53 delete-vpc-association-authorization \
--hosted-zone-id PRIVATE_ZONE_ID \
--vpc VPCRegion=us-east-1,VPCId=vpc-0aaaabbbbccccddddA zone must always keep at least one associated VPC. Removing the last association fails with a LastVPCAssociation error. To remove the last VPC you must delete the zone.
Split-horizon DNS
Split-horizon DNS means one name returns different answers depending on where the query comes from. You build it by creating a public hosted zone and a private hosted zone with the same name, for example example.com in both.
- Queries from the internet are answered from the public zone.
- Queries from associated VPCs are answered from the private zone only.
For instance, api.example.com can resolve to a public load balancer address for customers and to a private address for your own instances.
Overlapping zones and the most specific match
Two private zones can have overlapping names, such as example.com and internal.example.com, and both can be associated with the same VPC. The Resolver picks the zone whose name is the longest match for the query. A query for db.internal.example.com goes to internal.example.com. A query for www.example.com goes to example.com. This lets different teams own different subdomains.
You cannot associate one VPC with two private zones that have the exact same name. The same name in one private and one public zone is fine, because the private zone wins for that VPC.
Routing policies and health checks in private zones
Private zones support the same routing policy fields as public zones, including weighted, latency, failover and geolocation-style records. A weighted pair for a gradual internal rollout looks the same as the public example from the routing chapter.
Health checks need care. Route 53 health checkers run on the public internet and cannot reach a private IP address such as 10.0.1.25. To fail over private records, create a CloudWatch alarm that measures the endpoint from inside the VPC (for example a metric published by a script or a load balancer target health metric). Then create a Route 53 health check of type CLOUDWATCH_METRIC that follows that alarm, and attach it to the record with HealthCheckId.
Testing and troubleshooting
Test from inside an associated VPC, on an instance or a bastion host:
dig db.internal.example.com A +short
dig @169.254.169.253 db.internal.example.com AThe second command queries the Resolver directly and removes any doubt about which DNS server the instance is using. Testing from your laptop proves nothing, because your laptop is outside the VPC and will see NXDOMAIN or a public answer.
| Symptom | Likely cause | Fix |
|---|---|---|
| NXDOMAIN for a name that exists in the private zone | The VPC is not associated with the zone, or `enableDnsHostnames` is off | Check `list-hosted-zones-by-vpc` and the VPC attributes |
| NXDOMAIN for a public name that worked before | A private zone with the same name was associated and lacks that record | Add the record to the private zone, or use a different private zone name |
| Names resolve in one VPC but not a peered VPC | Peering does not share zones | Associate the second VPC with the zone |
| Instance ignores the private zone | Custom DHCP options or `resolv.conf` point to another DNS server | Point the instance at the VPC Resolver, or forward to it |
| On-premises hosts cannot resolve the names | The Resolver is only reachable from inside the VPC | Use a Resolver inbound endpoint (next chapter) |
| Old answer after a change | The previous record's TTL has not expired in a cache | Wait for the TTL, and use low TTLs before planned changes |
To list the private zones a VPC is associated with:
aws route53 list-hosted-zones-by-vpc \
--vpc-id vpc-0123456789abcdef0 \
--vpc-region us-east-1Exercise
Your company has a public zone and a private zone, both named example.com, with the private zone associated with your production VPC. A new instance in that VPC cannot resolve www.example.com, which exists only in the public zone. Why does it fail, and what are two ways to fix it?
Show answer
The private zone shadows the public zone for associated VPCs. The Resolver finds a matching private zone, looks for www in it, and returns NXDOMAIN instead of falling back to public DNS. One fix is to add a www record to the private zone with the answer VPC clients should get. The other is to rename the private zone to a more specific name, such as internal.example.com, so that queries for www.example.com no longer match it.
Exercise
You peered VPC A (associated with internal.example.com) and VPC B (not associated). An instance in VPC B can ping 10.0.1.25 but cannot resolve db.internal.example.com. What do you run to fix it?
Show answer
Peering carries traffic but not private zone associations. Run aws route53 associate-vpc-with-hosted-zone with the private zone ID and VPC B's region and ID. If VPC B is in another account, first run create-vpc-association-authorization from the zone owner's account, then run the association from VPC B's account.