5 min read

AWS in Practice - DNS Load Balancing with Route 53

1. A Different Kind of Load Balancing

In the previous article of this series, we launched EC2 instances and grouped them into a target group, ready to receive traffic from a traditional load balancer. But there’s a decision layer that happens before any packet reaches an Application Load Balancer: name resolution. When a client asks “what’s the IP of api.mycompany.com?”, whoever answers can already be choosing, based on latency, target health or a weighting policy, which region or endpoint to send that client to. It’s this kind of balancing — done in Route 53, AWS’s DNS service — that we’ll explore here.

2. Why Balance in DNS If There’s Already a Load Balancer

An ALB/NLB distributes traffic across instances within a region. Route 53 solves a different problem: distributing traffic across regions, across accounts or across completely different services, before a TCP connection is even opened. The two aren’t competitors — in practice, it’s common for Route 53 records to point precisely at ALBs in different regions, forming two layers of load balancing:

Client → Route 53 (picks the region) → Regional ALB → Target Group → EC2

The core difference in mechanism matters: an ALB decides per request, in real time, looking at the current state of each target. Route 53 decides per DNS query response, and the client (or, more often, its DNS resolver) keeps that answer cached for as long as the record’s TTL says — which makes DNS load balancing coarser, but indispensable when the requirement is geographic or crosses boundaries a load balancer alone can’t reach.

3. Route 53 Routing Policies

Route 53 supports several routing policies for the same name, each solving a different problem:

  • Simple: one record, one fixed answer (or a list, picked at random by the client). No real balancing.
  • Weighted: distributes queries across multiple endpoints by relative weight — useful for canary releases or for gradually shifting traffic from one environment to another.
  • Latency-based: answers with the endpoint in the region with the lowest latency measured from the location of the resolver that asked.
  • Failover: keeps a primary and a secondary endpoint, switching automatically when the primary’s health check fails.
  • Geolocation / Geoproximity: routes based on the geographic location of whoever is asking, useful for data residency requirements or localized content.
  • Multivalue answer: returns several healthy answers per query, working like DNS round-robin with a built-in health check.

4. Weighted Routing in Practice

To gradually shift traffic between two ALBs (for example, an old and a new version of the application), two records with the same name and different weights:

aws route53 change-resource-record-sets \
  --hosted-zone-id Z1234567890ABC \
  --change-batch '{
    "Changes": [{
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "api.mycompany.com",
        "Type": "A",
        "SetIdentifier": "current-version",
        "Weight": 90,
        "AliasTarget": {
          "HostedZoneId": "Z35SXDOTRQ7X7K",
          "DNSName": "old-alb-123456.us-east-1.elb.amazonaws.com",
          "EvaluateTargetHealth": true
        }
      }
    }]
  }'

And the second record, pointing at the new ALB with 10% of the traffic:

aws route53 change-resource-record-sets \
  --hosted-zone-id Z1234567890ABC \
  --change-batch '{
    "Changes": [{
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "api.mycompany.com",
        "Type": "A",
        "SetIdentifier": "new-version",
        "Weight": 10,
        "AliasTarget": {
          "HostedZoneId": "Z35SXDOTRQ7X7K",
          "DNSName": "new-alb-654321.us-east-1.elb.amazonaws.com",
          "EvaluateTargetHealth": true
        }
      }
    }]
  }'

Gradually increasing the weight of “new-version” (10 → 50 → 100) and reducing “current-version” by the same proportion is a simple way to do a canary release without relying on routing logic in the application.

5. The Same Scenario in Terraform

The AWS provider models each weighted record as an aws_route53_record with a set_identifier and a weighted_routing_policy block:

resource "aws_route53_record" "api_current" {
  zone_id        = aws_route53_zone.main.zone_id
  name           = "api.mycompany.com"
  type           = "A"
  set_identifier = "current-version"

  weighted_routing_policy {
    weight = 90
  }

  alias {
    name                   = aws_lb.old.dns_name
    zone_id                = aws_lb.old.zone_id
    evaluate_target_health = true
  }
}

resource "aws_route53_record" "api_new" {
  zone_id        = aws_route53_zone.main.zone_id
  name           = "api.mycompany.com"
  type           = "A"
  set_identifier = "new-version"

  weighted_routing_policy {
    weight = 10
  }

  alias {
    name                   = aws_lb.new.dns_name
    zone_id                = aws_lb.new.zone_id
    evaluate_target_health = true
  }
}

6. Automatic Failover with a Health Check

For a disaster recovery scenario — a primary environment and a standby in another region — Route 53 monitors the primary endpoint and only resolves to the secondary when it fails:

aws route53 create-health-check \
  --caller-reference "hc-api-primary-$(date +%s)" \
  --health-check-config '{
    "IPAddress": "203.0.113.10",
    "Port": 443,
    "Type": "HTTPS",
    "ResourcePath": "/healthz",
    "RequestInterval": 30,
    "FailureThreshold": 3
  }'

The returned HealthCheckId is referenced in both failover records:

resource "aws_route53_record" "primary" {
  zone_id         = aws_route53_zone.main.zone_id
  name            = "api.mycompany.com"
  type            = "A"
  set_identifier  = "primary"
  health_check_id = aws_route53_health_check.primary.id

  failover_routing_policy {
    type = "PRIMARY"
  }

  alias {
    name                   = aws_lb.main_region.dns_name
    zone_id                = aws_lb.main_region.zone_id
    evaluate_target_health = true
  }
}

resource "aws_route53_record" "secondary" {
  zone_id        = aws_route53_zone.main.zone_id
  name           = "api.mycompany.com"
  type           = "A"
  set_identifier = "secondary"

  failover_routing_policy {
    type = "SECONDARY"
  }

  alias {
    name                   = aws_lb.dr_region.dns_name
    zone_id                = aws_lb.dr_region.zone_id
    evaluate_target_health = true
  }
}

As long as the primary’s health check is healthy, every query gets the main endpoint. As soon as three consecutive checks fail, Route 53 starts answering with the secondary endpoint — with no manual intervention, although the full effect depends on the record’s TTL and how well clients’ resolvers respect that cache.

7. An Important Caveat: TTL and Propagation

Unlike an ALB, which reacts to changes in a target’s health within seconds, a change in a Route 53 answer only reaches new clients as the record’s TTL expires in each intermediate resolver — and some resolvers ignore very low TTLs. For failover scenarios, it’s common to use a 30 to 60 second TTL on critical records, accepting a longer window of partial unavailability than a layer 4/7 load balancer would have, in exchange for the ability to route between entire regions.

8. Conclusion

Combining the two layers — EC2 instances grouped into a target group, as we saw in the previous article, and Route 53 routing policies on top of them — covers both fine-grained balancing within a region and higher-level decisions: gradual canary releases, latency reduction by geography and failover between entire regions. The choice of policy (weighted, latency-based, failover) depends on the problem: a gradual version migration calls for weighted, a global presence calls for latency-based, disaster recovery calls for failover — and nothing stops you from combining more than one, since Route 53 allows nested policies that point one set of records at another.


References:

  1. Amazon Route 53 — Developer Guide
  2. Route 53 — Choosing a Routing Policy
  3. Terraform Provider: AWS — aws_route53_record