count and for_each

The count and for_each meta-arguments create multiple instances of a resource or module. count relies on a number and an index, for_each on a collection or a map of values identified by key. for_each is safer when elements are added or removed, because it avoids shifting indexes.

Example

resource "aws_s3_bucket" "b" { for_each = toset(["a", "b"]) bucket = "proj-${each.key}" }

Related concepts

The whole glossary