I have a situation where I want to create a number of env
blocks for a kubernetes_deployment. The scenario is that I’m writing a re-usable module, that our apps can use, we pass in a variable, say use_email = true
. Based on this var, the env
block is created, or not. My idea was to use the likes of:
locals = {
email_vars = {
"DJANGO_EMAIL_HOST" : "mail_smtp_endpoint"
"DJANGO_EMAIL_PORT" : "mail_smtp_port"
"DJANGO_EMAIL_USER" : "mail_smtp_user"
"DJANGO_EMAIL_PASSWORD" : "mail_smtp_password"
"DJANGO_EMAIL_DOMAIN" : "mail_smtp_domain"
}
}
dynamic "env" {
for_each = (var.use_email) ? local.email_vars : []
content {
name = env.key
value_from {
secret_key_ref {
name = local.environment_secret_name
key = env.value
}
}
}
}
However, I am getting the error:
Error: Inconsistent conditional result types
│
│ on .terraform/modules/app_deploy/terraform/modules/application-deploy/main.tf line 310, in resource "kubernetes_deployment" "app_deployment":
│ 310: for_each = (var.use_email) ? local.email_vars : []
│ ├────────────────
│ │ local.email_vars is object with 5 attributes
│ │ var.use_email is a bool, known only after apply
│
│ The true and false result expressions must have consistent types. The given expressions are object and tuple,
│ respectively.
and I’m not sure how to progress, any advice would be welcome, thanks in advance.