In my azuread_user data source, I would like to expand on each.value instead of just grabbing the first index (each.value[0] so that I can process all UIDs. I have been banging my head trying to do this with nested for expressions and have the requirement to preserve the index key, i.e. AWAS-EXC-Hybrid-Subnet-testing for later lookups
I managed to figure this out, perhaps in not the most DRY way but thought to share in case it helps someone or if someone can suggest another way. I think it is down to how deep these for expressions can go within a resource/data source. Locals seem to offer more flexibility, even though it is suggested to use them sparingly.
So here goes:
First a flattened set of aws_group_owner_ids to feed the datasource:
flat_aws_group_owner_ids = toset(flatten([
for k, v in local.aws_group_owner_ids : [
for l in local.aws_group_owner_ids[k] : l
]
]))
Then I grab the owner emails from the data source by the index key and preserve the key by declaring another local:
owner_emails = [for k, v in local.aws_group_owner_ids : {
for o in local.aws_group_owner_ids[k] : k => data.azuread_user.aws_owners[o].mail...
}
Lastly I figured out to cast using tomap and use merge to collapse the tuple, in order to have a single map for further referencing in for expressions :
aws_owner_emails = tomap(merge([for k, v in local.aws_group_owner_ids : {
for o in local.aws_group_owner_ids[k] : k => data.azuread_user.aws_owners[o].mail...
}
]...))
Wishing someday for HCL debugging support, would be helpful to step into code to see what’s going on
Nice, this is much more elegant thank you. One thing I wanted to understand is that when we build the below expression, the way you do it: k => [for o in v: data.azuread_user.aws_owners[o].mail]
What are we telling the compiler to do when expression for the desired index key is in brackets?