HashiCorp Terraform Associate (004) · Section 5 · 10% of the exam
Describe variable scope within modules
One of the objectives of the HashiCorp Certified: Terraform Associate (004) exam, in the section “Terraform modules”. Here is what it covers, the points to remember and a question to try.
What it covers
A child module is a sealed unit: it sees only the values passed in as arguments and exposes only its outputs. This lesson covers input and output flow, local values and resources being private, module meta-arguments (count, for_each, providers, depends_on), provider inheritance, module addresses and refactoring with moved blocks.
Points to remember
- Child modules see only the arguments passed in; the caller sees only the child's outputs (module.NAME.OUTPUT).
- Resources, locals and data sources inside a module are private; -var, tfvars and TF_VAR_ set root variables only.
- A missing required argument fails the plan; an undeclared argument gives Unsupported argument.
- Sibling modules connect through the root: output of one becomes input of the other.
- Module blocks accept count, for_each, providers and depends_on (count and for_each are mutually exclusive); version works only for registry sources.
Try a question
A module database outputs endpoint. The app module needs it, and the root also needs it for a DNS record. The team wants to reduce duplication. Which is the best configuration?
- AReference
module.database.endpointin theappmodule block and the DNS resource, both in the root - BHave
appcalldatabaseitself to get the endpoint - CDefine the endpoint as a local value in the root and read it from
app - DAdd
depends_on = [module.database]toappand readmodule.database.endpointinside theappmodule's own resources
Show the answer
A. Composition happens in the root: outputs become inputs of other modules. References create the implicit dependency graph.
Written for practice in the style of the exam: never a question from the real exam.
In the course
The lesson Using modules and variable scope teaches this objective (about 45 minutes), and 23 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.