HashiCorp Terraform Associate (004) Question 145
Single answer5b Describe variable scope within modulesYour team maintains a reusable Terraform module in ./modules/network that declares:
variable "environment" { type = string }
A root module calls it like this:
module "network" { source = "./modules/network" }
The root module also has a terraform.tfvars file containing:
environment = "prod"
When running terraform plan from the root module, Terraform prompts for a value for var.environment used by the child module. The team wants the child module to receive the root value without being prompted, while keeping the module reusable. What is the best fix?
- A
Add environment = var.environment inside the module "network" block, and declare variable "environment" in the root module.
- B
Move terraform.tfvars into ./modules/network so the child module can automatically load its own variable values.
- C
Reference var.environment directly inside the child module without declaring it, because input variables are inherited from the root module.
- D
Add an output for environment in the root module, and reference that output from inside the child module.
Show answer and explanation
Correct answer: A
Explanation
Terraform modules have isolated input variable scope. The root module can use values from terraform.tfvars because Terraform loads variable values for the root working directory. Child modules do not automatically read the root module's variable values, tfvars files, or outputs. Instead, the caller must pass values explicitly through the module block, such as environment = var.environment. This is a core Terraform module design principle and is documented in HashiCorp guidance for input variables and module composition: child modules define inputs with variable blocks, and parent modules supply those values explicitly when calling the module. This explicit interface improves reuse, readability, and predictability.
- A. Correct.
Correct. Input variables are scoped to the module where they are declared. A child module does not automatically receive values from the root module's variables or tfvars files. To pass a value into a child module, the root module must declare its own variable "environment" and explicitly map it in the module block with environment = var.environment. This keeps the child module reusable because callers can provide different values.
- B. Incorrect.
Incorrect. Terraform automatically loads variable definition files such as terraform.tfvars for the root module where Terraform is executed, not separately for child modules. Placing terraform.tfvars inside the child module directory does not cause Terraform to auto-load those values for that module. This is a common misconception about module-local variable files.
- C. Incorrect.
Incorrect. Variables are not inherited across module boundaries. If the child module declares variable "environment", that variable must be assigned by the caller through the module block, given a default, or entered interactively. Simply referencing var.environment in the child module does not pull from the root module's variable scope.
- D. Incorrect.
Incorrect. Outputs flow outward from a module to its caller, not inward from the root module to a child module. A child module cannot reference root module outputs directly. The correct pattern is explicit input passing through arguments in the module block.