Question 1 · choose 1
A root module declares variable "environment". A child module's resources reference var.environment, and terraform validate reports that no such variable is declared in the child module. What is the fix?
- AReference it in the child module's resources as parent.var.environment
- BAdd the variable to a locals block in the child module
- CDeclare it in the child and pass it as an argument of the module block
- DMark the root module's variable with global = true
Show the answer and why
AReference it in the child module's resources as parent.var.environment
Incorrect
There is no parent reference; a child sees only its own variables.
BAdd the variable to a locals block in the child module
Incorrect
A local value is not an input, and it still cannot read the parent's variable.
CDeclare it in the child and pass it as an argument of the module block
Correct
Child modules receive their inputs from the parent as arguments of the module block, which correspond to variables declared in the child.
DMark the root module's variable with global = true
Incorrect
Variable blocks have no global argument; every module declares its own inputs.
Variables do not leak downward; each module declares its inputs, and the caller passes them in.
HashiCorp documentation
- References to named values (opens in a new tab)
- Local values (when to use them) (opens in a new tab)
- Input variables (assigning values and precedence) (opens in a new tab)
- Manage values in modules (variables, locals, outputs) (opens in a new tab)
- variable block reference (arguments) (opens in a new tab)