Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 70 ยท Networking with VPC

Route 53 and DNS

Learn how DNS works and how Route 53 uses hosted zones, record types, alias records, routing policies (weighted, latency, failover, geolocation) and health checks.

People remember names, and networks route by numbers. The Domain Name System (DNS) is the global phone book that turns www.example.com into an IP address. Amazon Route 53 is AWS's DNS service: it can register domain names, host the records for a domain, and also answer queries in clever ways, sending users to the nearest Region, shifting a fraction of traffic to a new version, or failing over to a backup when a server stops responding.

You cannot register a domain or edit a real zone on this site, so the AWS commands and console steps below are read-only. The runnable examples are stand-ins: a small resolver that follows CNAME and alias records through a zone, and a simulator that applies weighted and failover routing. They follow the real rules, but they are models, not Route 53.

How a DNS lookup works

When your browser needs www.example.com, a recursive resolver (provided by your network or a public service) does the legwork. It asks a root server where .com is handled, asks the .com servers where example.com is handled, and finally asks the domain's authoritative name servers for the answer. It caches the answer for the record's TTL (time to live, in seconds) so repeated lookups are fast.

Route 53 plays the authoritative role. When you create a hosted zone for example.com, Route 53 gives you four name server (NS) records. You register these at the domain's registrar, which can be Route 53 itself or another company, and from then on Route 53 answers for the domain.

sequenceDiagram
    participant B as Browser
    participant R as Recursive resolver
    participant Root as Root servers
    participant TLD as .com servers
    participant A as Route 53<br/>authoritative
    B->>R: www.example.com?
    R->>Root: who handles .com?
    Root-->>R: the .com servers
    R->>TLD: who handles example.com?
    TLD-->>R: the Route 53 name servers
    R->>A: www.example.com?
    A-->>R: 203.0.113.10, TTL 300
    R-->>B: 203.0.113.10

The resolver walks from the root to the authoritative servers, then caches the answer for the TTL.

Hosted zones

A hosted zone is a container for the records of one domain. There are two kinds:

  • A public hosted zone answers queries from anywhere on the internet.
  • A private hosted zone answers only for the VPCs you associate with it. It lets you use names such as db.internal.example.com for private addresses, even for a domain that also has a public zone, a pattern called split-horizon DNS. VPC DNS support must be enabled for it to work, as covered in VPC Fundamentals.

A hosted zone costs a small monthly fee, and queries are billed per million. Deleting a zone does not delete the domain registration, and deleting a zone you still point to will break the site.

Record types you will use

Each record has a name, a type, a TTL and a value.

Type Maps a name to Example use
A An IPv4 address www to 203.0.113.10
AAAA An IPv6 address The same, over IPv6
CNAME Another name blog to sites.example.net
MX Mail servers, with priority Email delivery
TXT Free text Domain verification, SPF and DKIM
NS The zone's name servers Delegation
SOA Zone administration data Created automatically
CAA Which authorities may issue certificates Restrict certificate issuers
SRV A service's host and port Some service discovery
PTR An address back to a name Reverse lookups

A CNAME says "this name is an alias for that name", but it has two limits: it cannot coexist with other records of the same name, and it cannot be placed at the zone apex (example.com itself, the "naked" domain), because the apex must hold the SOA and NS records. That is a problem when you want the bare domain to point to a load balancer, whose address is a name rather than a fixed IP.

Alias records

Route 53 solves that with alias records, a Route 53-specific extension. An alias record looks like an A or AAAA record to the outside world, but its value is an AWS resource, such as a CloudFront distribution, an Application Load Balancer, an API Gateway endpoint, an S3 static website or another record in the same zone. Route 53 looks up the current addresses of the target itself and returns them.

Alias records have properties that make them the preferred way to point at AWS resources:

  • They work at the zone apex.
  • Queries to alias records pointing at AWS resources are not charged.
  • They follow the target as its IP addresses change, so there is no TTL to babysit.
  • They can use the target's health, with the "evaluate target health" setting.

Use a CNAME to point at a name outside AWS, and an alias for AWS resources. The resolver below follows both kinds through a zone. It is a model of the idea: it answers with fixed addresses, where Route 53 would look up live ones.

Python A tiny DNS resolver with CNAME and alias

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Routing policies

A plain record returns the same answer to everyone. A routing policy lets Route 53 choose among several records that share a name. You attach the policy to each record, and Route 53 evaluates it for every query.

  • Simple: one record, or several values returned together. No health checks.
  • Weighted: each record has a weight, and traffic is split in that proportion. Weights of 90 and 10 send about 10 percent of queries to the second record, a handy way to test a new version or migrate gradually. A weight of 0 stops sending traffic.
  • Latency: answers with the record for the AWS Region that gives the user the lowest network latency, measured by AWS.
  • Failover: a primary record is returned while its health check passes, and a secondary record when it fails. This implements active-passive recovery, which High Availability and Disaster Recovery builds on.
  • Geolocation: chooses by the user's location, from continent to country (or US state), with a default record for everyone else. It suits content licensing, language and compliance rules.
  • Geoproximity: chooses by the distance between the user and your resources, with a bias you can tune to shift more or less traffic to a location.
  • Multivalue answer: returns up to eight healthy records at random, a basic form of client-side balancing. It is not a replacement for a load balancer.
  • IP-based: chooses by the client's network range, for example to route a particular internet provider's users to a specific endpoint.

Health checks

A health check makes Route 53 itself probe an endpoint from several locations around the world, over HTTP, HTTPS or TCP, at an interval of 30 seconds (or 10 for a fast check). It marks the endpoint unhealthy after a configurable number of failures, by default three. There are also calculated health checks that combine others, and checks that follow a CloudWatch alarm, which lets you fail over on a private resource that Route 53's probes cannot reach.

A failed check changes which records Route 53 returns, but it cannot stop resolvers that have already cached the old answer. That is why records used for failover should have a short TTL, such as 30 to 60 seconds, accepting a few more queries in exchange for faster switching. Alias records pointing at healthy AWS resources avoid this by evaluating target health directly.

The simulator below applies weighted and failover routing. A seeded random generator keeps the output reproducible, so the results are the same on every run. In a real zone the choice varies from one query to the next.

Python Weighted and failover routing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting it together

A typical production setup: a public hosted zone for example.com; an alias A record at the apex and for www pointing at a CloudFront distribution (see CloudFront CDN); api as a latency or failover record set that points at load balancers in two Regions; MX and TXT records for email; and a private hosted zone for internal names. The CLI creates records through a change batch:

Terminal
# read-only: add an alias record for the apex
aws route53 change-resource-record-sets \
  --hosted-zone-id Z0123456789ABC \
  --change-batch '{
    "Changes": [{
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "example.com.",
        "Type": "A",
        "AliasTarget": {
          "HostedZoneId": "Z32O12XQLNTSW2",
          "DNSName": "alb-1234.eu-west-1.elb.amazonaws.com.",
          "EvaluateTargetHealth": true
        }
      }
    }]
  }'

The HostedZoneId inside AliasTarget is the zone ID of the target service, not your own, and each service and Region has a fixed value that you look up in its documentation.

DNS changes are not instant: resolvers hold old answers until the TTL expires, so lower the TTL a day before a planned change. Mistakes in DNS are some of the highest-impact errors in operations, because a wrong record can take every service on a domain offline at once. Make changes through infrastructure as code, as shown in CloudFormation, and review them like any other code.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why can you not use a CNAME record for the bare domain example.com?

    A CNAME must be the only record for its name, but the apex always holds SOA and NS records. Route 53 alias records solve this for AWS targets.
  2. 2. Which is an advantage of an alias record pointing at an AWS resource?

    Alias records resolve the target's current addresses, are allowed at the apex, and standard queries to alias records for AWS resources are free.
  3. 3. You want 5 percent of users to try a new version. Which routing policy fits?

    Weighted routing splits queries in proportion to the weights, so it supports gradual rollouts.
  4. 4. Why should failover records have a short TTL?

    A health check changes what Route 53 returns, but cached answers persist until the TTL runs out. A short TTL reduces that window.
  5. 5. What is the purpose of a private hosted zone?

    A private hosted zone resolves names only inside the VPCs you associate with it, which suits internal names for private addresses.

Summary

  • DNS resolves names through root, top-level and authoritative servers, and caches answers for the TTL; Route 53 is the authoritative service.
  • Public hosted zones answer the internet; private hosted zones answer only associated VPCs.
  • CNAME cannot be used at the apex; alias records can, and they are the right way to point at AWS resources.
  • Routing policies (weighted, latency, failover, geolocation and others) choose among records with the same name.
  • Health checks drive failover, but cached answers persist for the TTL, so keep failover TTLs short.

Next: S3 Buckets and Objects.