Courseiva
Read, generate and modify configurationmediumMultiple ChoiceObjective-mapped

TF-004 Read, generate and modify configuration Practice Question

A DevOps engineer needs to generate multiple similar AWS EC2 instances from a single resource block. They want each instance to have a unique name tag based on an index. Which approach should they use?

⚠ Common exam trap

The TF-003 exam often tests the distinction between `count` and `for_each` by presenting a scenario that requires index-based naming, leading candidates to incorrectly choose `for_each` because they confuse key-based iteration with index-based iteration.

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 `count` and reference `count.index` for the name tag

The `count` meta-argument creates multiple resources from a single resource block, and `count.index` provides a zero-based index for each instance. This index can be used directly in the name tag, such as `"instance-${count.index}"`, to generate unique names. `count` is the simplest and most appropriate mechanism when you need a fixed number of similar resources differentiated by an index.

Answer analysis

Option-by-option breakdown

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

  • Use a `locals` block to define multiple resources

    Why it's wrong here

    A `locals` block in Terraform is used to define named values or expressions that can be referenced throughout the configuration, enhancing readability and reducing repetition. However, `locals` are purely for defining computed values; they do not possess the functionality to instantiate or generate actual infrastructure resources like AWS EC2 instances. Resource creation is exclusively handled by dedicated resource blocks, not by local value definitions.

  • Use `for_each` and reference `each.key` for the name tag

    Why it's wrong here

    While `for_each` is a powerful meta-argument for creating multiple resources, it iterates over a map or a set of strings, requiring distinct keys or values to define each instance. For the specific requirement of generating "multiple similar" EC2 instances where the primary differentiator is a simple numerical sequence, `count` is generally the more direct and idiomatic solution. `for_each` is typically preferred when each resource needs to be configured based on unique, non-sequential identifiers from an input collection, rather than just an index.

  • Use a `terraform_data` resource to loop

    Why it's wrong here

    The `terraform_data` resource is designed to manage arbitrary data within Terraform state, facilitating data transformations or enabling interaction with external processes. Its purpose is to store and retrieve data, or to trigger operations that produce data, not to dynamically create or replicate other infrastructure resources such as `aws_ec2_instance` blocks. It lacks the inherent capability to loop and provision multiple instances of a defined resource type.

  • Use `count` and reference `count.index` for the name tag

    Why this is correct

    The `count` meta-argument is specifically designed for creating multiple instances of a resource based on a numerical index. By setting `count` to the desired number of instances, Terraform creates that many resources, each accessible via `count.index` (a zero-based integer). This index can then be directly incorporated into resource attributes, such as a name tag, to uniquely identify each "similar" EC2 instance, perfectly fulfilling the requirement.

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.