How do you use `for_each` with a list of objects?
IntermediateConceptLoops, Conditionals, Dynamic Blocks and Functions
Answer
Convert the list to a map with a for expression using a stable unique key.
locals {
rules = { for r in var.rule_list : r.name => r }
}
resource "aws_security_group_rule" "r" {
for_each = local.rules
from_port = each.value.port
# ...
}
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`?IntermediateWhat does "The for_each value depends on resource attributes that cannot be determined until apply" mean?AdvancedHow do you create a resource conditionally?BeginnerWhat is the conditional expression syntax?Beginner