I'm trying to create local variable for for_each loop in other resource, but can't make a local map as expected.
The following is what I tried. (Terraform 0.12)
Expected map to loop
temple_list = { "test2-role" = "test-test2-role", ... }
main.tf
locals {
tmpl_list = flatten([
for role in keys(var.roles) : {
for tmpl_vars in values(var.roles[role].tmpl_vars) :
role => "test-${role}" if tmpl_vars == "SECRET"
}
])
}
output "tmpl_list" {
value = local.tmpl_list
}
variable "roles" {
type = map(object({
tmpl_vars = map(string)
tags = map(string)
}))
default = {
"test-role" = {
tmpl_vars = {test = "y"}
tags = { test = "xxx"}
}
"test2-role" = {
tmpl_vars = { token = "SECRET" }
tags = {}
}
"test3-role" = {
tmpl_vars = {test = "x"}
tags = {}
}
}
}
Error comes from merge
| var.roles is map of object with 3 elements
Call to function "merge" failed: arguments must be maps or objects, got
"tuple".
Without merge
locals {
tmpl_list = flatten([
for role in keys(var.roles) : {
for tmpl_vars in values(var.roles[role].tmpl_vars) :
role => "test-${role}" if tmpl_vars == "SECRET"
}
])
}
output "tmpl_list" {
value = local.tmpl_list
}
variable "roles" {
type = map(object({
tmpl_vars = map(string)
tags = map(string)
}))
default = {
"test-role" = {
tmpl_vars = {test = "y"}
tags = { test = "xxx"}
}
"test2-role" = {
tmpl_vars = { token = "SECRET" }
tags = {}
}
"test3-role" = {
tmpl_vars = {test = "x"}
tags = {}
}
}
}
Result of no merge
tmpl_list = [
{},
{
"test2-role" = "test-test2-role"
},
{},
]
I tried other functions like tomap(), but it seems not working for me. (Also I'm not sure why empty ones are created.)
How can I convert this tuple to a map without empty ones?