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?
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"]
To access a map output from a module, use the standard map index syntax with brackets and the key name. Option A correctly uses module.sg["web-sg"] to retrieve the security group ID for 'web-sg'. Option B is invalid because dot notation cannot be used with a map key containing a hyphen. Option C incorrectly uses data source syntax; module outputs are accessed via module.<name>.<output> directly. Option D uses an index, which is only valid for list-type outputs, not maps.
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.
Visual reference
Go deeper
Related to this question
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 →
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.