July 10, 2020

Managing multiple DNS zones with Terraform and Google Cloud DNS

Google Cloud DNS is a convenient way to manage DNS Zones. With Terraform, it’s possible to manage these Zones as code, usually by committing the terraform files to version control. However, setting up sub-zones has always been a bit tricky. I’ll use this blog post to document how to manage zones and sub-zones with CloudDNS and terraform.

To manage a zone, the resource google_dns_managed_zone can be used:

To add a sub-zone:

Now we have two zones, but the sub-zone is not reachable. Due to how the DNS works, we have to add NS records to the “top” zone, that point to the sub-zone:

This record resides in the “top-level” zone my-domain.com, and points to the sub-zone subdomain.my-domain.com. By using the attribute name_servers of the google_dns_managed_zone resource, we can connect these two zones within terraform. GCP defines the DNS servers for each zone, by using the name_servers attribute we can dynamically refer to the used dns servers.

Please note: the above examples require Terraform 0.12+.