HashiCorp Terraform Associate (004) Question 141
Single answer5b Describe variable scope within modulesYour team maintains a reusable Terraform module named network that declares variable "region" {} and uses it to configure resources. In the root module, you already have variable "region" { default = "us-west-2" } and call the module like this:
module "network" { source = "./modules/network" }
When you run terraform plan, Terraform prompts for module.network.var.region. You want the child module to use the root module's region value without being prompted, while keeping the module reusable. What is the best way to fix this?
- A
Pass the value explicitly in the module block:
module "network" { source = "./modules/network" region = var.region } - B
Remove
variable "region" {}from the child module so it automatically inherits the root module variable of the same name - C
Create a
terraform.tfvarsfile insidemodules/networkso the child module reads the region value directly - D
Add
locals { region = var.region }in the root module so all child modules can accesslocal.regionautomatically
Show answer and explanation
Correct answer: A
Explanation
Terraform modules have isolated scope boundaries. Variables declared in one module are not automatically visible in another module, including between the root module and child modules. A child module must declare its own input variables, and the caller must pass values through the module block. The same principle applies to local values: they are only available within the module that defines them. In practice, the root module acts as the composition layer, wiring values into child modules explicitly. This behavior is documented in Terraform's module and input variable documentation, which describes modules as having their own input and output interfaces rather than sharing variable scope implicitly.
- A. Correct.
Correct. Input variables are scoped to the module where they are declared. A child module does not automatically see variables from the root module, even if the names match. To provide a value to a child module input variable, you must pass it explicitly in the
moduleblock, such asregion = var.region. This preserves module reusability because the module still defines its own input contract and the caller decides what value to pass. - B. Incorrect.
Incorrect. Child modules do not inherit root module input variables by name. If you remove the variable declaration from the child module but still reference
var.regioninside it, Terraform will fail because that variable is undeclared in the child module. This reflects a common misconception that variable names create shared scope across modules. - C. Incorrect.
Incorrect. Terraform automatically loads
.tfvarsfiles for the root module, not for child modules as a way to satisfy module inputs. A child module should receive values from its caller through arguments in themoduleblock. Placingterraform.tfvarsinside the child module directory is not the standard mechanism for setting that module's inputs during normal use. - D. Incorrect.
Incorrect. Locals are also scoped to the module where they are defined. Defining
local.regionin the root module can help avoid repetition within the root module, but child modules still cannot access that local value unless it is passed explicitly as a module argument, for exampleregion = local.region.