Resource: aws_route53_record

Provides a Route53 record resource.

Example Usage

Simple routing policy

resource "aws_route53_record" "www" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "www.example.com"
  type    = "A"
  ttl     = 300
  records = [aws_eip.lb.public_ip]
}

Weighted routing policy

Other routing policies are configured similarly. See Amazon Route 53 Developer Guide for details.

resource "aws_route53_record" "www-dev" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "www"
  type    = "CNAME"
  ttl     = 5

  weighted_routing_policy {
    weight = 10
  }

  set_identifier = "dev"
  records        = ["dev.example.com"]
}

resource "aws_route53_record" "www-live" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "www"
  type    = "CNAME"
  ttl     = 5

  weighted_routing_policy {
    weight = 90
  }

  set_identifier = "live"
  records        = ["live.example.com"]
}

Geoproximity routing policy

resource "aws_route53_record" "www" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "www.example.com"
  type    = "CNAME"
  ttl     = 300
  geoproximity_routing_policy {
    coordinates {
      latitude  = "49.22"
      longitude = "-74.01"
    }
  }
  set_identifier = "dev"
  records        = ["dev.example.com"]
}

Alias record

See related part of Amazon Route 53 Developer Guide to understand differences between alias and non-alias records.

TTL for all alias records is 60 seconds, you cannot change this, therefore ttl has to be omitted in alias records.

resource "aws_elb" "main" {
  name               = "foobar-terraform-elb"
  availability_zones = ["us-east-1c"]

  listener {
    instance_port     = 80
    instance_protocol = "http"
    lb_port           = 80
    lb_protocol       = "http"
  }
}

resource "aws_route53_record" "www" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "example.com"
  type    = "A"

  alias {
    name                   = aws_elb.main.dns_name
    zone_id                = aws_elb.main.zone_id
    evaluate_target_health = true
  }
}

NS and SOA Record Management

When creating Route 53 zones, the NS and SOA records for the zone are automatically created. Enabling the allow_overwrite argument will allow managing these records in a single Terraform run without the requirement for terraform import.

resource "aws_route53_zone" "example" {
  name = "test.example.com"
}

resource "aws_route53_record" "example" {
  allow_overwrite = true
  name            = "test.example.com"
  ttl             = 172800
  type            = "NS"
  zone_id         = aws_route53_zone.example.zone_id

  records = [
    aws_route53_zone.example.name_servers[0],
    aws_route53_zone.example.name_servers[1],
    aws_route53_zone.example.name_servers[2],
    aws_route53_zone.example.name_servers[3],
  ]
}

Argument Reference

This resource supports the following arguments:

Exactly one of records or alias must be specified: this determines whether it's an alias record.

Alias

Alias records support the following:

CIDR Routing Policy

CIDR routing policies support the following:

Failover Routing Policy

Failover routing policies support the following:

Geolocation Routing Policy

Geolocation routing policies support the following:

GeoproximityRouting Policy

Geoproximity routing policies support the following:

Latency Routing Policy

Latency routing policies support the following:

Weighted Routing Policy

Weighted routing policies support the following:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import Route53 Records using the ID of the record, record name, record type, and set identifier. For example:

Using the ID of the record, which is the zone identifier, record name, and record type, separated by underscores (_):

import {
  to = aws_route53_record.myrecord
  id = "Z4KAPRWWNC7JR_dev.example.com_NS"
}

If the record also contains a set identifier, append it:

import {
  to = aws_route53_record.myrecord
  id = "Z4KAPRWWNC7JR_dev.example.com_NS_dev"
}

If the record name is the empty string, it can be omitted:

import {
  to = aws_route53_record.myrecord
  id = "Z4KAPRWWNC7JR__NS"
}

Using terraform import to import Route53 Records using the ID of the record, record name, record type, and set identifier. For example:

Using the ID of the record, which is the zone identifier, record name, and record type, separated by underscores (_):

% terraform import aws_route53_record.myrecord Z4KAPRWWNC7JR_dev.example.com_NS

If the record also contains a set identifier, append it:

% terraform import aws_route53_record.myrecord Z4KAPRWWNC7JR_dev.example.com_NS_dev