Hi,
I’m trying to use “for each” within resources.
Passing a map as a value I have no problems, but if I try to organize the data with another structure, I always get the same error: (panic: not a string).
I have reviewed the posts in this forum but I have not found a solution.
Ejem:
I’m sure it can be done but I need some help because I’m blocked.
Thank you in advance!!
This is the first example, passing a map works fine:
locals {
server_name = "test9999"
elastic_pool_name = "ep9999"
location = "westus2"
resource_name = "test"
test = {
ddbb1 = "SQL_Latin1_General_CP1_CI_AS,10737418240"
ddbb2 = "SQL_Latin1_General_CP1_CI_AS,10737418240"
}
}
resource "azurerm_sql_database" "db" {
for_each = local.test
name = each.key
resource_group_name = local.rg_name
location = local.location
server_name = local.server_name
collation = element(split(",", each.value), 0)
max_size_bytes = element(split(",", each.value), 1)
elastic_pool_name = local.elastic_pool_name
}
but if I try to create a more complex data structure (this one for example), I get the same result (panic: not a string)
variable "ddbbs" {
default = {
common = {
resource_name = "test"
location = "westus2"
server_name = "test9999"
elastic_pool_name = "ep9999"
}
ddbbs = [
{
name = "ddbb1"
collation = "SQL_Latin1_General_CP1_CI_AS"
size = "10737418240"
},
{
name = "ddbb2"
collation = "SQL_Latin1_General_CP1_CI_AS"
size = "10737418240"
},
]
}
}
locals {
elastic_pool_name = lookup(var.ddbbs["common"], "elastic_pool_name", "")
server_name = lookup(var.ddbbs["common"], "server_name", "")
rg_name= lookup(var.ddbbs["common"], "resource_name", "")
location= lookup(var.ddbbs["common"], "location", "")
ddbbs = var.ddbbs["ddbbs"]
flat_data = flatten([
for z in local.ddbbs : {
name = z.name
collation = z.collation
size = z.size
}
])
}
resource "azurerm_sql_database" "db" {
for_each = local.flat_data
name = each.value.name
resource_group_name = local.rg_name
location = local.location
server_name = local.server_name
collation = each.value.collation
max_size_bytes = each.value.size
elastic_pool_name = local.elastic_pool_name
}