Sub topic 9
DNS resolution and hostnames inside a VPC
This chapter assumes you have read the earlier chapters on VPCs, CIDR blocks, and subnets. You should also be comfortable with the AWS CLI and with basic DNS record types (A, CNAME). It explains how instances resolve names, which two VPC settings control that, and where DNS usually breaks.
The Amazon-provided resolver
Every VPC has a built-in DNS server called the Route 53 Resolver, also known as AmazonProvidedDNS. You can reach it from inside the VPC at two addresses:
- The VPC CIDR base address plus two. For
10.0.0.0/16this is10.0.0.2. - The link-local address
169.254.169.253, which works regardless of the VPC range.
Instances do not need a route to it. The traffic is handled by the network fabric, so it works even in a subnet with no internet gateway or NAT gateway. The default DHCP option set tells instances to use this resolver.
The resolver answers three kinds of queries:
- Internal EC2 names, such as
ip-10-0-1-25.ec2.internal. - Records in private hosted zones attached to the VPC.
- Public names, which it resolves recursively using public DNS.
Each network interface can send at most 1,024 packets per second to the resolver. Workloads that make many uncached lookups can hit this limit. The symptom is intermittent resolution failures under load.
The two VPC attributes
Two VPC attributes control DNS behavior.
| Attribute | What it does | Default for `create-vpc` | Default VPC |
|---|---|---|---|
| `enableDnsSupport` | Turns the Amazon-provided resolver on or off for the VPC | true | true |
| `enableDnsHostnames` | Gives instances with public IPs a public DNS hostname and enables private DNS features | false | true |
If enableDnsSupport is false, the resolver at .2 stops answering. Instances can then resolve names only through a custom DNS server you provide. If enableDnsHostnames is false, instances in a VPC you created get no public DNS hostname.
Check and set both attributes with the CLI. modify-vpc-attribute accepts one attribute per call.
aws ec2 describe-vpc-attribute --vpc-id "$VPC_ID" --attribute enableDnsSupport
aws ec2 describe-vpc-attribute --vpc-id "$VPC_ID" --attribute enableDnsHostnames
aws ec2 modify-vpc-attribute --vpc-id "$VPC_ID" --enable-dns-support
aws ec2 modify-vpc-attribute --vpc-id "$VPC_ID" --enable-dns-hostnamesSeveral features need both attributes set to true:
- Private hosted zones associated with the VPC.
- The private DNS option on interface VPC endpoints.
- Public DNS hostnames for instances that have a public IP.
A private hosted zone that seems to be ignored is most often a VPC with one of these attributes turned off.
Hostnames EC2 assigns
Each instance gets a private DNS name based on its private IPv4 address. The format depends on the Region:
| Region | Private DNS name |
|---|---|
| us-east-1 | `ip-10-0-1-25.ec2.internal` |
| Other Regions | `ip-10-0-1-25.us-west-2.compute.internal` |
An instance with a public IPv4 address and enableDnsHostnames set also gets a public name, such as ec2-203-0-113-25.compute-1.amazonaws.com.
This public name behaves differently depending on where you query it:
- From inside the VPC, it resolves to the instance's private IP. Traffic between instances stays on the private network and does not leave through the internet gateway.
- From the internet, it resolves to the public IP.
This split-horizon behavior is why you should use DNS names instead of hard-coded IPs when instances talk to each other.
Instances can also use resource-based names (i-0abc123.ec2.internal). You choose between IP-based and resource-based naming per subnet.
How a query is resolved
The resolver checks sources in a fixed order. A private hosted zone that matches a name takes precedence over public DNS for that name.
If a private hosted zone matches a domain but has no record for the exact name, the resolver returns NXDOMAIN. It does not fall back to public DNS. This catches many people who create a private zone for example.com and then lose access to www.example.com, which only exists publicly. Add the missing records to the private zone.
Private hosted zones
A private hosted zone gives you your own internal names, such as db.corp.internal, that resolve only inside the VPCs you associate with it. Create one with your VPC attached, then add a record.
ZONE_ID=$(aws route53 create-hosted-zone \
--name corp.internal \
--caller-reference "corp-internal-$(date +%s)" \
--vpc VPCRegion=us-east-1,VPCId="$VPC_ID" \
--hosted-zone-config Comment="Internal names",PrivateZone=true \
--query HostedZone.Id --output text)
cat > record.json <<'EOF'
{
"Changes": [{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "db.corp.internal",
"Type": "A",
"TTL": 60,
"ResourceRecords": [{ "Value": "10.0.2.15" }]
}
}]
}
EOF
aws route53 change-resource-record-sets \
--hosted-zone-id "$ZONE_ID" \
--change-batch file://record.jsonFrom an instance in the VPC, test the name against the resolver:
dig +short db.corp.internal @10.0.0.2To share the zone with another VPC, associate it with aws route53 associate-vpc-with-hosted-zone. If the VPC is in a different account, you must first authorize the association with create-vpc-association-authorization.
DHCP option sets
A DHCP option set defines the DNS settings the VPC hands to instances. The settings are the DNS servers, the domain name, and the NTP servers. A VPC uses exactly one option set at a time. The default sets domain-name-servers to AmazonProvidedDNS.
You can switch to a custom option set to use your own DNS servers. Doing so has these consequences:
- Your custom servers handle all queries, so instances no longer resolve private hosted zone names unless your servers forward to
10.0.0.2. - Existing instances pick up the change only when they renew their DHCP lease or restart networking.
- You cannot edit an option set after creating it. Create a new one and associate it with the VPC.
If you only need to resolve some domains differently, a Resolver rule (below) is usually simpler than replacing the DNS server.
Hybrid DNS with Resolver endpoints
By default, the VPC resolver cannot query your on-premises DNS, and on-premises servers cannot query the VPC. Route 53 Resolver endpoints fix both directions:
- An inbound endpoint gives on-premises DNS servers an IP address in your VPC to send queries to. They can then resolve private hosted zone names.
- An outbound endpoint sends queries from the VPC to your on-premises DNS servers. A forwarding rule selects which domains it handles, for example
corp.example.com.
Each endpoint is a set of elastic network interfaces in at least two subnets. You attach a security group that allows DNS on TCP and UDP port 53. The on-premises side must be reachable over a VPN or Direct Connect connection, which later chapters cover.
DNS across peered VPCs and endpoints
Two other cases come up often:
- VPC peering. Resolving a peer's public DNS hostname to its private IP requires enabling DNS resolution on the peering connection, for each side of it. Without this option the name resolves to the public IP, and traffic fails or takes the wrong path.
- Interface endpoints. With private DNS enabled, the service's regular name (such as
sqs.us-east-1.amazonaws.com) resolves to the endpoint's private IPs inside the VPC. Your code needs no changes. This depends on both VPC DNS attributes being true.
Troubleshooting checklist
- Run
digagainst10.0.0.2directly. If that works but the default lookup fails, check/etc/resolv.confand the DHCP option set. - Confirm both VPC attributes with
describe-vpc-attribute. - For private zones, confirm the VPC is associated with the zone and that a matching zone does not hide a public name.
- Check whether the instance's security group or network ACL blocks outbound TCP/UDP 53 to custom DNS servers. Traffic to the Amazon resolver is not filtered by these.
- Look for throttling if failures only appear under heavy load.
Exercise
An instance in your VPC resolves app.example.com correctly from the public internet. You then create a private hosted zone for example.com, associate it with the VPC, and add only a record for db.example.com. Afterwards, the instance can no longer resolve app.example.com. Why, and how do you fix it?
Show answer
The private hosted zone matches the example.com domain, so the resolver answers from it and does not fall back to public DNS. Because the zone has no app.example.com record, the resolver returns NXDOMAIN. Fix it by adding an app.example.com record to the private zone, with the same value as the public record.
Exercise
You created a VPC with aws ec2 create-vpc and associated a private hosted zone with it. Instances cannot resolve the zone's records. enableDnsSupport is true. What do you check next, and what command fixes it?
Show answer
Check enableDnsHostnames, which is false by default for VPCs created with create-vpc. Private hosted zones need both attributes set to true. Verify with aws ec2 describe-vpc-attribute --vpc-id "$VPC_ID" --attribute enableDnsHostnames, then fix it with aws ec2 modify-vpc-attribute --vpc-id "$VPC_ID" --enable-dns-hostnames.