HashiCorp Terraform Associate (004) Question 143
Single answer5b Describe variable scope within modulesA team maintains a root Terraform configuration that deploys networking and then calls a child module named "app". The root module defines variable "environment" {} in variables.tf and sets environment = "prod" in terraform.tfvars. Inside the app child module, a resource is tagged with Environment = var.environment, but the child module does not declare its own variable "environment" {} block. During terraform plan, Terraform reports that the input variable is not declared in the module. What is the best fix?
- A
Add
variable "environment" {}to the child module and passenvironment = var.environmentin the module block from the root module. - B
Keep the child module unchanged because child modules automatically inherit all root module variables, including values from
terraform.tfvars. - C
Add
locals { environment = var.environment }in the root module so the child module can referencelocal.environmentdirectly. - D
Move
terraform.tfvarsinto the child module directory so the child module can load the variable value without any module input.
Show answer and explanation
Correct answer: A
Explanation
Terraform modules have explicit variable scope boundaries. Input variables, local values, and outputs belong to the module where they are declared. The root module can load variable values from sources such as terraform.tfvars, environment variables, CLI flags, and variable definition files, but child modules do not automatically inherit those values. Instead, the caller must pass values to a child module through arguments in the module block, and the child module must declare matching input variables. This design keeps modules reusable and predictable. HashiCorp documentation on modules and input variables emphasizes that child modules receive data only from their caller and expose data back through outputs.
- A. Correct.
Correct. Variables are scoped to the module where they are declared. A child module cannot directly access input variables declared in the root module. To use
environmentinside the child module, the child must declarevariable "environment" {}and the root module must explicitly pass a value in themoduleblock, for examplemodule "app" { source = "./app" environment = var.environment }. - B. Incorrect.
Incorrect. This reflects a common misconception about module scope. Child modules do not automatically inherit root module input variables or values loaded from
terraform.tfvars. Only the root module automatically receives values from CLI variable sources such asterraform.tfvars; child modules receive values only through arguments in the module block. - C. Incorrect.
Incorrect. Locals are also scoped to the module where they are declared. A child module cannot directly reference
local.environmentfrom the root module. Locals can help organize values within a module, but they do not bypass module input boundaries. - D. Incorrect.
Incorrect.
terraform.tfvarsis used for the root module's input variables when Terraform runs from that working directory. Terraform does not automatically load a child module's ownterraform.tfvarsas module inputs. Child modules are intended to receive inputs from their caller via themoduleblock, not from separate tfvars loading inside the module source directory.