Home > OS >  Terraform loop through 2 tuples
Terraform loop through 2 tuples

Time:10-28

In the below terraform code I am trying to create groups with single block. Now I am using 0,1 2 separate blocks to create. Is there any other method to create group with single block. I tried with flatten but of no luck

    locals {
      instances = [
        {
          instance  = "test1"
          baseUrl   = "url"
          subDomain = "sd"
          groups = [
            "app1",
            "app2",
        "app3",
      ]
},
    {
      instance  = "test2"
      baseUrl   = "url2"
      subDomain = "sd2"
      groups = [
        "t1",
        "t2",
        "t3",
      ]
},
  ]
}
resource "okta_group" "press" {
  for_each = { for k, instance in local.instances[0].groups : k => instance ]
  name     = each.value
}
resource "okta_group" "press1" {
  for_each = { for k, instance in local.instances[1].groups : k => instance ]
  name     = each.value
}

CodePudding user response:

In simple terms: You need to provide a single (flat) list to for_each. It doesn't accept list of lists or any other data structure.

Try:

for_each = { for k, instance in flatten(local.instances[*].groups) : k => instance}

which uses:

I suggest reading up on these.

  • Related