Courseiva
Understand IaC conceptsmediumMultiple ChoiceObjective-mapped

TF-004 Understand IaC concepts Practice Question

A team uses Terraform to manage multiple AWS EC2 instances with a 'count' meta-argument. They need to reference the ID of the third instance in another resource's configuration. Which expression should they use?

⚠ Common exam trap

The trap here is that candidates often forget Terraform uses zero-based indexing and mistakenly pick option D (index 3) or option A (index 1), confusing the ordinal position with the index number.

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

aws_instance.example[2].id

In Terraform, the 'count' meta-argument creates a list of resources indexed starting from 0. To reference the third instance, you use index 2 (0, 1, 2). Option B correctly uses `aws_instance.example[2].id` to access the ID of the third EC2 instance.

Answer analysis

Option-by-option breakdown

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

  • aws_instance.example[1].id

    Why it's wrong here

    This expression attempts to retrieve the ID of the instance at index `1` within the `aws_instance.example` collection. In Terraform, list and collection indices are zero-based, meaning `[1]` refers to the *second* element in the sequence. Therefore, if the goal is to identify the third EC2 instance, this specific index is incorrect for that purpose.

  • aws_instance.example[2].id

    Why this is correct

    This expression correctly references the ID of the third EC2 instance in a collection named `aws_instance.example`. Terraform consistently uses zero-based indexing for lists and resource collections created with `count`. Consequently, an index of `2` precisely targets the third element in the sequence, making `aws_instance.example[2].id` the correct way to retrieve its unique identifier.

  • element(aws_instance.example.*.id, 3)

    Why it's wrong here

    The `element` function is used to retrieve a single item from a list or tuple at a specified index. While `aws_instance.example.*.id` correctly generates a list of all instance IDs, providing an index of `3` to the `element` function will return the *fourth* item in that list, due to Terraform's zero-based indexing. This does not correspond to the third instance as implied by the question.

  • aws_instance.example[3].id

    Why it's wrong here

    This expression attempts to access the ID of the instance located at index `3` within the `aws_instance.example` collection. Given Terraform's standard zero-based indexing for lists and resource collections, an index of `3` actually corresponds to the *fourth* element in the sequence. Consequently, this would retrieve the fourth instance's ID, not the third one, making it an incorrect selection.

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.