DNS: Cloud DNS Managed Zones and Record Sets
Scenario
Your project owns the domain example.com and you want Google Cloud DNS to act as the authoritative name server. You need to create a public Managed Zone and add an A Record that maps the root domain to the public IP address of an existing Load Balancer or VM.
- Create a google_dns_managed_zone with the dns_name example.com. (Note the trailing dot).
- Create a google_dns_record_set of type `A` for the managed zone.
- Set the record's TTL to 300 (5 minutes) and map it to a dummy IP (e.g. 34.120.45.67).
- Output the Managed Zone name servers assigned by Google.
Additional Context
Record Sets: Ensure that the `name` attribute of a DNS record set exactly matches (or is a subdomain of) the `dns_name` of the zone, and that it ends with a dot indicating the DNS root. For example, `name = "example.com."`.
Private vs Public Zones: Cloud DNS can also host internal Split-Horizon DNS inside a VPC without exposing the resolution to the public internet by adding a `private_visibility_config` block and attaching networks to the zone.
variables.tf
variable "project_id" { type = string }
variable "domain_name" { type = string; default = "example.com." }
variable "web_ip" { type = string; default = "34.120.45.67" }main.tf
provider "google" {
project = var.project_id
}
# The Public Managed Zone
resource "google_dns_managed_zone" "my_zone" {
name = "example-com-zone"
dns_name = var.domain_name
description = "Public zone for example.com"
# Ensure visibility is public (default)
visibility = "public"
}
# The DNS Record Set
resource "google_dns_record_set" "root_a_record" {
# Name must match the zone's dns_name for the root record
name = google_dns_managed_zone.my_zone.dns_name
managed_zone = google_dns_managed_zone.my_zone.name
type = "A"
ttl = 300
rrdatas = [
var.web_ip
]
}outputs.tf
output "name_servers" {
value = google_dns_managed_zone.my_zone.name_servers
description = "Provide these to your Domain Registrar to delegate DNS to Google"
}Workflow Commands
terraform init
terraform apply -var="project_id=my-gcp-project-123"
# View the zone delegation name servers via gcloud
gcloud dns managed-zones describe example-com-zone
# View the records in the zone
gcloud dns record-sets list --zone=example-com-zone
terraform destroy -var="project_id=my-gcp-project-123"Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Outputs: name_servers = tolist([ "ns-cloud-a1.googledomains.com.", "ns-cloud-a2.googledomains.com.", "ns-cloud-a3.googledomains.com.", "ns-cloud-a4.googledomains.com.", ])

