Courseiva
Interact with Terraform moduleseasyMultiple ChoiceObjective-mapped

TF-004 Interact with Terraform modules Practice Question

A developer wants to create multiple instances of a module that provisions a single EC2 instance. They want to create 3 EC2 instances. Which approach is most efficient and concise?

⚠ Common exam trap

In the Terraform Associate exam, candidates are often tested on understanding that `count` is the most concise and efficient way to create a fixed number of identical module instances, while `for_each` is better for creating instances from a set of distinct keys or when you need to manage each instance independently with a unique identifier.

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

Use the 'count' meta-argument in the module block.

The `count` meta-argument in a module block allows you to create multiple instances of the entire module (including its EC2 instance) by specifying a number. This is the most efficient and concise approach when you need a fixed number of identical module instances, as it avoids code duplication and leverages Terraform's built-in iteration.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Set the source argument to a list of three URLs.

    Why it's wrong here

    The `source` argument within a module block is designed to accept a single string value, which specifies the path or URL to the module's source code. Terraform expects this argument to point to a singular location for module definition, indicating *what* module to use, not *how many* instances to create. Providing a list of three URLs would result in a configuration parsing error, as it fundamentally misinterprets the argument's purpose.

  • Use the 'count' meta-argument in the module block.

    Why this is correct

    The `count` meta-argument is specifically designed to create multiple identical instances of a resource or module block. By setting `count` to an integer value, such as `3`, Terraform will instantiate the module three times, creating separate, distinct instances. This method is highly efficient and concise for provisioning a fixed number of similar components, as it avoids repetition and leverages Terraform's built-in lifecycle management for each instance.

  • Use the 'for_each' meta-argument with a list of numbers.

    Why it's wrong here

    Using `for_each` with a list of numbers would iterate over each number in the list, but the module itself provisions only a single EC2 instance per iteration, so three numbers would yield three instances. This approach is tempting because `for_each` is commonly used to create multiple resources from a set of distinct keys, and it would work correctly here if the goal were to create three identical instances. However, the question asks for the *most efficient and concise* method; `for_each` requires explicitly defining a list (e.g., `[1, 2, 3]`), whereas the `count` meta-argument achieves the same result with a single integer, making it more concise for creating a fixed number of identical resources.

  • Copy the module block three times with different names.

    Why it's wrong here

    Copying and pasting the entire module block three times, each with a unique name, directly violates the Don't Repeat Yourself (DRY) principle. While functionally it would create three instances, this approach is highly inefficient, error-prone, and significantly reduces maintainability. Any future changes to the module's configuration would require identical modifications across all three blocks, increasing the risk of inconsistencies and configuration drift.

About these practice questions

Courseiva writes every TF-004 question from scratch — 428 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.