I want to manage Cloudflare DNS zones and records by Terraform.
I have a lot of zones and records, so I design variable like this:
variable "domains" {
type = map(any)
default = {
"test.domain.com" = {
config = {
"account" = "account_name"
}
records = {
"record1" = "www|A|127.0.0.1"
"record2" = "www|A|127.0.0.2"
"record3" = "mail|MX|127.0.0.3"
}
}
"test2.domain.com" = {
config = {
"account" = "account_name"
}
records = {
"record1" = "www|A|10.0.0.1"
"record2" = "www|A|10.0.0.2"
"record3" = "mail|MX|10.0.0.3"
}
}
"testN.domain.com" = {
config = {
"account" = "account_name"
}
records = {
"record1" = "www|A|192.0.0.1"
"record2" = "www|A|192.0.0.2"
"record3" = "mail|MX|192.0.0.3"
}
}
}
}
So the main idea to pass all this data to the module using for_each.
For each domain create all records.
module "cloudflare" {
source = "./cloudflare"
for_each = var.domains
domain_name = each.key
name = element(split("|", each.value.records), 0)
type = element(split("|", each.value.records), 1)
content = element(split("|", each.value.records), 2)
}
Does someone faced with it?
Or maybe you can propose a workaround?
Thanks!