for_each

The for_each argument, which creates one instance of a resource or module per element of a map or set, addressed by key such as name["a"].

In more detail

Because each instance is identified by its key rather than its position, adding or removing one element leaves the others untouched. Keys must be known at plan time, so they cannot come from attributes that are only known after apply. Inside the block, each.key and each.value give the current element.

Example

resource "null_resource" "subnet" {
  for_each = var.subnets
  triggers = { name = each.value }
}

Related terms

Taught in

References

Certification courses