HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 142 of 223

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

HashiCorp Terraform Associate (004) Question 142

Single answer5b Describe variable scope within modules

A team maintains a root Terraform configuration that deploys networking and compute resources. They recently added a child module named "app" located in ./modules/app. The root module defines a variable environment in variables.tf and sets it through terraform.tfvars. Inside ./modules/app/main.tf, a developer references var.environment to tag resources, but the module does not declare its own variable "environment" {} block. When they run terraform plan, Terraform reports that the input variable is not declared in the module. What is the best way to fix this while following Terraform module variable scoping rules?

  1. A

    Declare variable "environment" {} inside the child module and pass the value from the root module using module "app" { environment = var.environment }.

  2. B

    Keep the child module unchanged because child modules automatically inherit all root module variables defined in terraform.tfvars.

  3. C

    Move terraform.tfvars into ./modules/app so the child module can read the variable directly without changing the module block.

  4. D

    Replace var.environment in the child module with local.environment because locals are shared across module boundaries.

Show answer and explanation

Correct answer: A

Explanation

Terraform modules have clear boundaries for inputs, outputs, variables, and locals. Root module input variables can be set through CLI flags, environment variables, or tfvars files, but those values do not automatically flow into child modules. Each child module must declare the input variables it expects, and the calling module must pass values explicitly in the module block. This design keeps modules reusable and predictable. According to Terraform module and input variable documentation, input variables are like function arguments: they are defined by the called module and supplied by the caller. Likewise, local values are only available within the module where they are declared. The correct fix is therefore to declare variable "environment" {} in ./modules/app and pass it from the root module with environment = var.environment.

  • A. Correct.

    Correct. Variables are scoped to the module where they are declared. A child module cannot directly access variables declared only in the root module. To use the value inside the child module, the child module must declare its own input variable, and the root module must explicitly pass a value through the module block, such as environment = var.environment.

  • B. Incorrect.

    Incorrect. This reflects a common misconception. Values from terraform.tfvars are used to assign input variables for the root module being executed, not automatically for child modules. Child modules receive values only through arguments in the corresponding module block.

  • C. Incorrect.

    Incorrect. terraform.tfvars is not a mechanism for independently feeding variables into child modules during a normal root module run. Terraform loads variable definitions for the root module from the working directory context. Child modules are designed to receive inputs explicitly from their caller, not by reading their own tfvars files in this scenario.

  • D. Incorrect.

    Incorrect. Locals are also module-scoped. A local value defined in one module is not visible in another module unless its value is passed through an output from one module and then into another module as an input. Simply changing var.environment to local.environment would not solve the scoping problem.

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