Help with "for_each" in resource

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 
}

Hi @jcmaasb!

Any time you see Terraform report a “panic”, that’s always a bug in Terraform: Terraform should either do what you asked or produce a proper error describing the problem.

With that said, would you mind opening a bug report issue to cover this? The bug report template includes requests various specific bits of information that the Terraform team will use to try to reproduce the problem you’ve seen and hopefully fix Terraform to not crash in this situation.

Thanks!

Hi @apparentlymart

I will do some more tests to ensure that it is not a mistake.
If the issue persists, I will follow its recommendations.

At the moment I see that @rohrerbhas the same issue

Many Thanks!!

Thanks for linking these @jcmaasb. I created a bug report this morning > https://github.com/hashicorp/terraform/issues/22560