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:
flatten
- an operation to convert list of lists to a list- and
[*]
- what they call a splat expression
I suggest reading up on these.