TF-004 Interact with Terraform modules Practice Question
A module outputs a map of security group IDs keyed by name. In the root module, a resource needs to reference the security group ID for the name 'web-sg'. How should the root configuration access this value?
⚠ Common exam trap
TF-004 often tests the distinction between map and list indexing, and the invalid use of dot notation for map keys with special characters.
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
✓
module.sg["web-sg"]
When a module outputs a map, the root configuration accesses values using map key syntax: module.<module_name>["<key>"]. Since the output is keyed by name, module.sg["web-sg"] correctly retrieves the security group ID for 'web-sg'. This is standard Terraform HCL syntax for map outputs.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
module.sg["web-sg"]
Why this is correct
This syntax correctly accesses an element within a map output named "sg" from a module. Terraform maps are key-value collections, and individual values are retrieved by enclosing their string key, such as "web-sg", in square brackets. This method is the standard and explicit way to reference a specific item when the key is known, ensuring the correct security group ID is retrieved.
- ✗
module.sg.web-sg
Why it's wrong here
This dot notation is incorrect for accessing elements within a map output. While dot notation is used for accessing attributes of an object or named arguments within a block, it is not valid for retrieving values from a map using its keys. Terraform interprets `web-sg` here as an attempt to access a direct attribute named `web-sg` on the `sg` output, which is not how map elements are referenced.
- ✗
data.module.sg["web-sg"]
Why it's wrong here
The `data.` prefix is specifically used to reference data sources, which are read-only views of existing infrastructure or external data. Module outputs, however, are values exposed by a Terraform module and are always accessed using the `module.` prefix, followed by the module's local name and the output's name. Combining `data.` with `module.` is syntactically invalid because a module output is not a data source.
- ✗
module.sg[0]
Why it's wrong here
This syntax attempts to access an element using a numeric index, which is appropriate for lists or tuples, but not for maps. Terraform maps are unordered collections where values are associated with unique string keys, not sequential integer positions. Attempting to use `[0]` on a map will result in a type mismatch error, as maps do not support zero-based indexing like lists do.
Go deeper
Related to this question
About these practice questions
Courseiva writes every TF-004 question from scratch — 434 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 →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official HashiCorp exam blueprint
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.