for_each
Last updated
What is for_each?
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.
Free resources to learn for_each
- Official docsTerraform Docs - for_each
- ArticleTerraform by Example - for_each
- VideoTerraform for loops
More in stage 3: Providers
for_each: common questions
Can I learn for_each in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain for_each in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this for_each tutorial for beginners?
It is lesson 19 of 110 in the free Terraform roadmap, in stage 3, Providers. If you are new to Terraform, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.