Sub topic 9
DNS resolution and hostnames inside a VPC
This chapter assumes you know basic DNS (A records, resolvers) and can run AWS CLI commands. You should also have the VPC from the earlier chapters, with its CIDR block, subnets, and route tables.
How DNS works inside a VPC
Every VPC has a built-in DNS resolver called the Amazon-provided DNS server, also known as Route 53 Resolver. It answers at two addresses:
- The VPC base address plus two. For a
10.0.0.0/16VPC this is10.0.0.2. - The link-local address
169.254.169.253.
Instances reach the resolver through their network interface. It does not appear in a route table, and security groups and network ACLs do not apply to this traffic. The resolver answers queries for public domain names, for names in Route 53 private hosted zones associated with the VPC, and for AWS-provided hostnames.
The two VPC DNS attributes
Two VPC attributes control this behavior.
| Attribute | Effect when enabled | Default in a new VPC |
|---|---|---|
| `enableDnsSupport` | The resolver at the base address plus two answers queries. | On |
| `enableDnsHostnames` | Instances with public IPs get a public DNS hostname. | Off for `create-vpc`, on for the default VPC |
Private hosted zones and interface endpoint private DNS need both attributes on. If enableDnsSupport is off, instances cannot resolve any names through the Amazon-provided resolver.
The first code example in this tutorial turned on hostnames with modify-vpc-attribute. You can inspect both attributes like this:
aws ec2 describe-vpc-attribute --vpc-id "$VPC_ID" --attribute enableDnsSupport
aws ec2 describe-vpc-attribute --vpc-id "$VPC_ID" --attribute enableDnsHostnamesEach call returns a JSON document with a Value of true or false.
AWS-provided hostnames
With enableDnsHostnames on, an instance gets names such as these:
- Private:
ip-10-0-1-25.ec2.internal(inus-east-1; other Regions use<region>.compute.internal). - Public, if it has a public IPv4 address:
ec2-203-0-113-25.compute-1.amazonaws.com.
The public hostname has a useful property. Queried from inside the VPC, it resolves to the instance's private IP. Queried from outside, it resolves to the public IP. Traffic between two instances in the VPC that use each other's public hostnames therefore stays on the private network.
Resolution flow
Private hosted zones
A Route 53 private hosted zone holds records that resolve only inside the VPCs you associate with it. Use one to give internal services stable names, such as db.internal.example.com, instead of hard-coding IP addresses.
ZONE_ID=$(aws route53 create-hosted-zone \
--name internal.example.com \
--caller-reference "$(date +%s)" \
--vpc VPCRegion=us-east-1,VPCId="$VPC_ID" \
--query HostedZone.Id --output text)Passing --vpc makes the zone private and associates it with your VPC in one step.
Common errors
- Private zone names do not resolve. Check that both DNS attributes are on and that the VPC is associated with the zone.
- No public hostname on an instance. Either
enableDnsHostnamesis off or the instance has no public IPv4 address. - Custom DHCP options break resolution. If you replace the DHCP option set with your own DNS servers, instances stop using the Amazon-provided resolver unless you list
AmazonProvidedDNS. - Resolver rate limit. Each network interface can send about 1,024 packets per second to the resolver. Heavy lookups can be throttled, so cache results in applications.
Exercise
An instance in your VPC can reach the internet but cannot resolve a name in a private hosted zone you associated with the VPC. describe-vpc-attribute shows enableDnsSupport is true and enableDnsHostnames is false. What should you change, and why?
Show answer
Enable enableDnsHostnames with aws ec2 modify-vpc-attribute --vpc-id "$VPC_ID" --enable-dns-hostnames. Private hosted zone resolution requires both attributes to be true. Also check that the instance's DHCP options still include AmazonProvidedDNS.