Summary
Hi, I’m looking for a way to flatten()
between setproduct()
and for_each
. I’ve provided examples stepping through my progress to make the query clearer. Thanks for your time.
Examples
Simple
For example, to iterate over a set, I would:
env = toset(["dev", "test", "qa", "staging", "prod"])
...
for_each = local.env
# "dev", "test", "qa", "staging", "prod"
Product
For example, to iterate over all possible combinations of two sets, I would:
env = toset(["dev", "test", "qa", "staging", "prod"])
area = toset(["front", "back", "mid"])
...
for_each = { for v in setproduct(local.env, local.area) : "${v[0]}-${v[1]}" => v }
# "front-dev", "front-test", "front-qa", "front-staging", "front-prod"
# "back-dev", "back-test", "back-qa", "back-staging", "back-prod"
# "mid-dev", "mid-test", "mid-qa", "mid-staging", "mid-prod"
Mixed
As for my query, how do I go about mixing flatten()
and setproduct()
within nested sets in for_each
for the desired output shown below?
env = {
"uat" = toset(["dev", "test", "qa"])
"live" = toset(["staging", "prod"])
}
area = toset(["front", "back", "mid"])
...
# Desired output ->
# "uat-front-dev", "uat-front-test", "uat-front-qa", "live-front-staging", "live-front-prod"
# "uat-back-dev", "uat-back-test", "uat-back-qa", "live-back-staging", "live-back-prod"
# "uat-mid-dev", "uat-mid-test", "uat-mid-qa", "live-mid-staging", "live-mid-prod"
Thanks,
Rishav