I’m a bit stumped on this and not sure if my approach is an anti-pattern.
I have currently working something like this:
terraform.tfvars
apps = {
"nomad" = {
name = "Nomad"
domain = "nomad"
# commented out as not working
# create_record = false
},
"consul" = {
name = "Consul"
domain = "consul"
}
}
apps.tf
resource "cloudflare_access_application" "apps" {
for_each = var.apps
name = each.value.name
domain = each.value.domain
<...>
}
dns.tf
resource "cloudflare_record" "cname" {
for_each = var.apps
name = each.value.domain
<...>
}
variables.tf
variable "apps" {
type = map(object({
name = string
domain = string
create_record = optional(bool)
)})
}
locals {
apps = defaults(var.apps, {
create_record = true
})
}
I want to have a map of objects but I want conditionals in the event that I do not want to create a DNS record, or do not want to create an application but want to create a DNS record.
I tried the conditional:
for_each = {
for name, app in var.apps : name => app
if app.create_record
}
but then I got an error:
The condition value is null. Conditions must either be true or false.
Ideally I would have liked to have something like a map object with nested objects for app_config and dns_config, but have been unable to get that to work either.
Edit: I have right now about 20 applications that I manage with cloudflare, and while they share many settings, a few may have something different so I’d like to not be super verbose in the terraform.tfvars file if it isn’t necessary.