HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 141 of 223

Terraform Associate 004. Associate level, HashiCorp. Free question with the correct answer and a full explanation.

HashiCorp Terraform Associate (004) Question 141

Single answer5b Describe variable scope within modules

Your 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?

  1. A

    Pass the value explicitly in the module block: module "network" { source = "./modules/network" region = var.region }

  2. B

    Remove variable "region" {} from the child module so it automatically inherits the root module variable of the same name

  3. C

    Create a terraform.tfvars file inside modules/network so the child module reads the region value directly

  4. D

    Add locals { region = var.region } in the root module so all child modules can access local.region automatically

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 module block, such as region = 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.region inside 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 .tfvars files 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 the module block. Placing terraform.tfvars inside 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.region in 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 example region = local.region.

Timed practice exam

Take a HashiCorp Terraform Associate (004) practice test under exam conditions

70 questions in 60 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam