Interview Prep/Terraform/Loops, Conditionals, Dynamic Blocks and Functions

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
  # ...
}
All Terraform questions Roadmap: Terraform

More on Loops, Conditionals, Dynamic Blocks and Functions