Using for_each with Terraform Modules
You have a list of VPC configurations and want to create multiple VPCs using a single module block with for_each. Which statement is true?
Quick Answer
The correct answer is that when using for_each with modules, the module’s outputs become a map of output values keyed by the for_each keys. This occurs because for_each creates one module instance per key in the provided map or set, and each instance produces its own set of outputs; Terraform then collects all those outputs into a single map, using the original for_each keys as the map keys. On the HashiCorp Terraform Associate TF-003 exam, this concept tests your understanding of how for_each transforms module outputs, often appearing in questions that contrast for_each with count or that ask about output structure. A common trap is assuming outputs remain a simple list or single value, but the key insight is that for_each always yields a map. Remember: for_each keys become output map keys—think “key to key” to avoid confusion.
⚠ Common exam trap
A common misconception tested on the Terraform certification exam is that `for_each` on modules is unsupported or requires special conditions, when in fact it is a standard feature since Terraform 0.13. The key behavior to remember is that outputs become a map.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
When using for_each, the module's outputs will be a map of output values for each instance.
When you use `for_each` on a module block, Terraform creates multiple instances of the module, and the module's outputs become a map of output values keyed by the `for_each` key. This allows you to access each instance's outputs via the map, enabling iteration over the created resources.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for_each on a module is not supported.
Why it's wrong here
Terraform has supported for_each on module blocks since version 0.13, so this claim is factually wrong. It is tempting because earlier Terraform releases genuinely lacked module-level for_each, and count on a module was the correct choice before that version for creating repeated module instances.
- ✗
You can use for_each on a module block only if the module is published in the registry.
Why it's wrong here
Registry publication is irrelevant; for_each works with any module source, including local paths and Git. It is tempting because registry modules are commonly used and versioned, but registry publication would be the correct consideration when pinning module versions, not when iterating over a map.
- ✓
When using for_each, the module's outputs will be a map of output values for each instance.
Why this is correct
With `for_each`, Terraform instantiates one module per map key, so each instance's outputs are collected into a map keyed by those same keys, letting you reference values per VPC. This satisfies the stem's single-module-block requirement, since outputs remain addressable individually rather than being flattened into a single list.
- ✗
for_each on a module block requires the module to support count.
Why it's wrong here
Module blocks accept for_each directly; they do not require the module to support count, which is a separate meta-argument. It is tempting because count and for_each are both repetition mechanisms, so count on a module would be the correct choice when creating identical instances distinguished only by index.
Go deeper
Related to this question
About these practice questions
One of 434 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on TF-004
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Given the plan output, how is the module 'instances' configured in the root module?
medium- ✓ A.The root module uses for_each in the module block with a map variable.
- B.The module uses count with a list variable.
- C.The module is declared twice with different names.
- D.The module contains a resource with for_each inside.
Why A: The explanation correctly describes the characteristics of a Terraform plan output when `for_each` is used on a module block with a map variable (key-value structure like 'instance1', 'instance2'). However, this description *is* the missing input from the STEM. The explanation's role is to justify the correct answer based on the *provided* STEM and options, not to provide missing context from the STEM itself. If the described plan output were provided in the STEM, the explanation would be accurate and the answer correct.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.