The for_each meta-argument in Terraform enables you to create multiple instances of a resource based on a set or map. Unlike count, which uses a simple integer, for_each allows for more granular and dynamic resource creation, as each instance is associated with a specific key-value pair from the given set or map. This meta-argument is particularly useful for creating resources with unique configurations derived from the keys and values of the set or map. By leveraging for_each, you can manage collections of resources more efficiently, ensuring each instance can be individually referenced and customized based on its specific key.
Note: You cannot declare for_each
and count
in the same resource.
Learn more from the following resources: