HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 143 of 223

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

HashiCorp Terraform Associate (004) Question 143

Single answer5b Describe variable scope within modules

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

  1. A

    Add variable "environment" {} to the child module and pass environment = var.environment in the module block from the root module.

  2. B

    Keep the child module unchanged because child modules automatically inherit all root module variables, including values from terraform.tfvars.

  3. C

    Add locals { environment = var.environment } in the root module so the child module can reference local.environment directly.

  4. D

    Move terraform.tfvars into 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 environment inside the child module, the child must declare variable "environment" {} and the root module must explicitly pass a value in the module block, for example module "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 as terraform.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.environment from the root module. Locals can help organize values within a module, but they do not bypass module input boundaries.

  • D. Incorrect.

    Incorrect. terraform.tfvars is used for the root module's input variables when Terraform runs from that working directory. Terraform does not automatically load a child module's own terraform.tfvars as module inputs. Child modules are intended to receive inputs from their caller via the module block, not from separate tfvars loading inside the module source directory.

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