How do you group items by a key using `for` expressions?
AdvancedConceptLoops, Conditionals, Dynamic Blocks and Functions
Answer
Use the ... grouping suffix, which collects values with the same key into a list.
locals {
by_az = { for s in var.servers : s.az => s.name... }
}
# {"a" = ["web1","web3"], "b" = ["web2"]}
Without ... duplicate keys raise an error.
More on Loops, Conditionals, Dynamic Blocks and Functions
How does `count` work?BeginnerHow does `for_each` work?BeginnerWhen should you choose `for_each` over `count`?IntermediateHow do you use `for_each` with a list of objects?IntermediateWhat does "The for_each value depends on resource attributes that cannot be determined until apply" mean?AdvancedHow do you create a resource conditionally?Beginner