Skip to content

Instantly share code, notes, and snippets.

@calexandre
Last active August 20, 2021 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calexandre/51cf64bf22619a76a169d01a83f2b405 to your computer and use it in GitHub Desktop.
Save calexandre/51cf64bf22619a76a169d01a83f2b405 to your computer and use it in GitHub Desktop.
GCP DNS mass creation with Terraform
locals {
# the default is important in case you need it elsewhere (it points to the first zone created)
dns_default_zone = "my-dns-zone"
dns_default_fqdn = "${local.dns_default_zone}.${data.google_dns_managed_zone.parent[local.dns_default_zone].dns_name}"
dns_zones = {
"${local.dns_default_zone}" = {
parent_zone_name = "my-parent-dns-zone"
}
}
}
data "google_dns_managed_zone" "parent" {
for_each = local.dns_zones
name = each.value.parent_zone_name
}
resource "google_dns_managed_zone" "this" {
for_each = local.dns_zones
name = "${each.key}-dns"
dns_name = "${each.key}.${data.google_dns_managed_zone.parent[each.key].dns_name}"
dnssec_config {
state = on
}
# Set this true to delete all records in the zone.
force_destroy = true
}
# ## this is required to register the NS records on the parent zone
resource "google_dns_record_set" "parent_ns" {
for_each = local.dns_zones
managed_zone = data.google_dns_managed_zone.parent[each.key].name
project = data.google_dns_managed_zone.parent[each.key].project
name = "${each.key}.${data.google_dns_managed_zone.parent[each.key].dns_name}"
type = "NS"
ttl = 300
rrdatas = google_dns_managed_zone.this[each.key].name_servers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment