google_dns_record_set

Manages a set of DNS records within Google Cloud DNS. For more information see the official documentation and API.

Example Usage

Binding a DNS name to the ephemeral IP of a new instance:

resource "google_dns_record_set" "frontend" {
  name = "frontend.${google_dns_managed_zone.prod.dns_name}"
  type = "A"
  ttl  = 300

  managed_zone = google_dns_managed_zone.prod.name

  rrdatas = [google_compute_instance.frontend.network_interface[0].access_config[0].nat_ip]
}

resource "google_compute_instance" "frontend" {
  name         = "frontend"
  machine_type = "g1-small"
  zone         = "us-central1-b"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  network_interface {
    network = "default"
    access_config {
    }
  }
}

resource "google_dns_managed_zone" "prod" {
  name     = "prod-zone"
  dns_name = "prod.mydomain.com."
}

Adding an A record

resource "google_dns_record_set" "a" {
  name         = "backend.${google_dns_managed_zone.prod.dns_name}"
  managed_zone = google_dns_managed_zone.prod.name
  type         = "A"
  ttl          = 300

  rrdatas = ["8.8.8.8"]
}

resource "google_dns_managed_zone" "prod" {
  name     = "prod-zone"
  dns_name = "prod.mydomain.com."
}

Adding an MX record

resource "google_dns_record_set" "mx" {
  name         = google_dns_managed_zone.prod.dns_name
  managed_zone = google_dns_managed_zone.prod.name
  type         = "MX"
  ttl          = 3600

  rrdatas = [
    "1 aspmx.l.google.com.",
    "5 alt1.aspmx.l.google.com.",
    "5 alt2.aspmx.l.google.com.",
    "10 alt3.aspmx.l.google.com.",
    "10 alt4.aspmx.l.google.com.",
  ]
}

resource "google_dns_managed_zone" "prod" {
  name     = "prod-zone"
  dns_name = "prod.mydomain.com."
}

Adding an SPF record

Quotes ("") must be added around your rrdatas for a SPF record. Otherwise rrdatas string gets split on spaces.

resource "google_dns_record_set" "spf" {
  name         = "frontend.${google_dns_managed_zone.prod.dns_name}"
  managed_zone = google_dns_managed_zone.prod.name
  type         = "TXT"
  ttl          = 300

  rrdatas = ["\"v=spf1 ip4:111.111.111.111 include:backoff.email-example.com -all\""]
}

resource "google_dns_managed_zone" "prod" {
  name     = "prod-zone"
  dns_name = "prod.mydomain.com."
}

Adding a CNAME record

The list of rrdatas should only contain a single string corresponding to the Canonical Name intended.

resource "google_dns_record_set" "cname" {
  name         = "frontend.${google_dns_managed_zone.prod.dns_name}"
  managed_zone = google_dns_managed_zone.prod.name
  type         = "CNAME"
  ttl          = 300
  rrdatas      = ["frontend.mydomain.com."]
}

resource "google_dns_managed_zone" "prod" {
  name     = "prod-zone"
  dns_name = "prod.mydomain.com."
}

Setting Routing Policy instead of using rrdatas

Weighted Round Robin

resource "google_dns_record_set" "wrr" {
  name         = "backend.${google_dns_managed_zone.prod.dns_name}"
  managed_zone = google_dns_managed_zone.prod.name
  type         = "A"
  ttl          = 300

  routing_policy {
    wrr {
      weight  = 0.8
      rrdatas =  ["10.128.1.1"]
    }

    wrr {
      weight  = 0.2
      rrdatas =  ["10.130.1.1"]
    }
  }

Geolocation

resource "google_dns_record_set" "geo" {
  name         = "backend.${google_dns_managed_zone.prod.dns_name}"
  managed_zone = google_dns_managed_zone.prod.name
  type         = "A"
  ttl          = 300

  routing_policy {
    geo {
      location = "asia-east1"
      rrdatas  =  ["10.128.1.1"]
    }

    geo {
      location = "us-central1"
      rrdatas  =  ["10.130.1.1"]
    }
  }
}

Failover

resource "google_dns_record_set" "a" {
  name         = "backend.${google_dns_managed_zone.prod.dns_name}"
  managed_zone = google_dns_managed_zone.prod.name
  type         = "A"
  ttl          = 300

  routing_policy {
    primary_backup {
      trickle_ratio = 0.1

      primary {
        internal_load_balancers {
          load_balancer_type = "regionalL4ilb"
          ip_address         = google_compute_forwarding_rule.prod.ip_address
          port               = "80"
          ip_protocol        = "tcp"
          network_url        = google_compute_network.prod.id
          project            = google_compute_forwarding_rule.prod.project
          region             = google_compute_forwarding_rule.prod.region
        }
      }

      backup_geo {
        location = "asia-east1"
        rrdatas  = ["10.128.1.1"]
      }

      backup_geo {
        location = "us-west1"
        rrdatas  = ["10.130.1.1"]
      }
    }
  }
}

resource "google_dns_managed_zone" "prod" {
  name     = "prod-zone"
  dns_name = "prod.mydomain.com."
}

resource "google_compute_forwarding_rule" "prod" {
  name   = "prod-ilb"
  region = "us-central1"

  load_balancing_scheme = "INTERNAL"
  backend_service       = google_compute_region_backend_service.prod.id
  all_ports             = true
  network               = google_compute_network.prod.name
  allow_global_access   = true
}

resource "google_compute_region_backend_service" "prod" {
  name   = "prod-backend"
  region = "us-central1"
}

resource "google_compute_network" "prod" {
  name = "prod-network"
}

Argument Reference

The following arguments are supported:


The routing_policy block supports:

The wrr block supports:

The geo block supports:

The primary_backup block supports:

The health_checked_targets block supports:

The internal_load_balancers block supports:

Attributes Reference

-In addition to the arguments listed above, the following computed attributes are -exported:

Import

DNS record sets can be imported using either of these accepted formats:

In Terraform v1.5.0 and later, use an import block to import DNS record sets using one of the formats above. For example:

import {
  id = "projects/{{project}}/managedZones/{{zone}}/rrsets/{{name}}/{{type}}"
  to = google_dns_record_set.default
}

When using the terraform import command, DNS record sets can be imported using one of the formats above. For example:

$ terraform import google_dns_record_set.default projects/{{project}}/managedZones/{{zone}}/rrsets/{{name}}/{{type}}
$ terraform import google_dns_record_set.default {{project}}/{{zone}}/{{name}}/{{type}}
$ terraform import google_dns_record_set.default {{zone}}/{{name}}/{{type}}

Note: The record name must include the trailing dot at the end.