Files
homelab/tf-dns.tf
T
2025-05-11 16:13:40 -04:00

52 lines
1.0 KiB
Terraform

data "http" "myip" {
url = "https://api.myip.com/"
}
locals {
myip = var.myip != "" ? var.myip : jsondecode(data.http.myip.response_body)["ip"]
}
data "cloudflare_zone" "this" {
for_each = var.domain_mappings
filter = {
name = each.key
account = {
id = var.cloudflare.account_id
}
}
}
locals {
records = flatten([
for zone, records in var.domain_mappings : [
for r, def in records :
{
"zone" : zone,
"record" : r,
"type" : def.type != null ? def.type : "A",
"ip" : def.ip != null ? def.ip : local.myip,
"zone_id" : data.cloudflare_zone.this[zone].zone_id,
}
]
])
record_comment = "Updated by Terraform"
}
output "myip" {
value = local.myip
}
resource "cloudflare_dns_record" "this" {
for_each = {
for d in local.records :
"${d.record}.${d.zone}" => d
}
zone_id = each.value.zone_id
comment = local.record_comment
content = each.value.ip
name = each.key
type = each.value.type
proxied = false
ttl = 1
}