diff --git a/terraform/cloudflare/main.tf b/terraform/cloudflare/main.tf index 8a3492b..de99f09 100644 --- a/terraform/cloudflare/main.tf +++ b/terraform/cloudflare/main.tf @@ -18,9 +18,12 @@ provider "cloudflare" { data "cloudflare_accounts" "default" {} -# output "accounts" { -# value = data.cloudflare_accounts.default.accounts[0] -# } +locals { + dns_records = { + for index, record in distinct(var.dns_records) : # 'distint' removes duplicate values from a list + tostring(index) => record + } +} resource "cloudflare_zone" "balsillie_net" { account_id = data.cloudflare_accounts.default.accounts[0].id @@ -34,22 +37,14 @@ resource "cloudflare_zone_dnssec" "balsillie_net" { zone_id = cloudflare_zone.balsillie_net.id } -resource "cloudflare_record" "a_records" { - for_each = var.a_records - zone_id = cloudflare_zone.balsillie_net.id - proxied = false - type = "A" - ttl = 60 - name = each.key - content = each.value +resource "cloudflare_record" "dns_records" { + for_each = local.dns_records + zone_id = cloudflare_zone.balsillie_net.id + proxied = false + name = each.value.name + type = each.value.type + content = each.value.content + ttl = each.value.ttl } -resource "cloudflare_record" "cname_records" { - for_each = var.cname_records - zone_id = cloudflare_zone.balsillie_net.id - proxied = false - type = "CNAME" - ttl = 60 - name = each.key - content = each.value -} \ No newline at end of file +# TODO update the SOA record when dns_records resource was changed \ No newline at end of file diff --git a/terraform/cloudflare/variable_definitions.tf b/terraform/cloudflare/variable_definitions.tf index fe2c83f..b85806e 100644 --- a/terraform/cloudflare/variable_definitions.tf +++ b/terraform/cloudflare/variable_definitions.tf @@ -5,20 +5,13 @@ variable "api_token" { sensitive = true } -variable "a_records" { +variable "dns_records" { description = "DNS A records to create" - type = map(string) - default = {} + type = list(object({ + name = string + type = string + content = string + ttl = number + })) + default = [] } - -variable "cname_records" { - description = "DNS CNAME records to create" - type = map(string) - default = {} -} - -variable "root_records" { - description = "Special root records to create with name @" - type = map(string) - default = {} -} \ No newline at end of file diff --git a/terraform/cloudflare/variables.auto.tfvars b/terraform/cloudflare/variables.auto.tfvars index e39db71..f67ea3a 100644 --- a/terraform/cloudflare/variables.auto.tfvars +++ b/terraform/cloudflare/variables.auto.tfvars @@ -1,35 +1,26 @@ -root_records = { - a = "5.161.254.39", -} - -a_records = { - www = "5.161.254.39" - cloud = "5.161.254.39" - cloud = "5.161.254.39" - hetzner = "5.161.254.39" - imap = "5.161.254.39" - sieve = "5.161.254.39" - smtp = "5.161.254.39" -} - -# test comment - -cname_records = { - auth = "hetzner" - autoconfig = "hetzner" - autodiscover = "hetzner" - code = "hetzner" - discord-bridge = "hetzner" - im = "hetzner" - matrix = "hetzner" - matrix-auth = "hetzner" - matrix-federation = "hetzner" - matrix-sync = "hetzner" - mta-sts = "hetzner" - notify = "hetzner" - office = "hetzner" - signal-bridge = "hetzner" - social = "hetzner" - turn = "hetzner" - whatsapp-bridge = "hetzner" -} \ No newline at end of file +dns_records = [ + { name = "@", type = "A", content = "5.161.254.39", ttl = 60 }, + { name = "www", type = "A", content = "5.161.254.39", ttl = 60 }, + { name = "cloud", type = "A", content = "5.161.254.39", ttl = 60 }, + { name = "hetzner", type = "A", content = "5.161.254.39", ttl = 60 }, + { name = "imap", type = "A", content = "5.161.254.39", ttl = 60 }, + { name = "sieve", type = "A", content = "5.161.254.39", ttl = 60 }, + { name = "smtp", type = "A", content = "5.161.254.39", ttl = 60 }, + { name = "auth", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "autoconfig", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "autodiscover", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "code", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "discord-bridge", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "im", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "matrix", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "matrix-auth", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "matrix-federation", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "matrix-sync", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "mta-sts", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "notify", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "office", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "signal-bridge", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "social", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "turn", type = "CNAME", content = "hetzner", ttl = 60 }, + { name = "whatsapp-bridge", type = "CNAME", content = "hetzner", ttl = 60 } +]