Courseiva
Use the core Terraform workflowhardMultiple ChoiceObjective-mapped

TF-004 Use the core Terraform workflow Practice Question

A developer is troubleshooting a Terraform configuration that fails during `terraform plan` with the error: "Error: Invalid reference". The error points to a line that references `var.environment` in a module block. What is the most likely cause?

⚠ Common exam trap

HashiCorp often tests the distinction between root module variables and child module input variables, trapping candidates who assume `var.environment` must be defined in the root module's `variables.tf` rather than in the child module's `variables.tf`.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

The variable `environment` is not defined in the child module's variables.tf

When a Terraform module block references a variable like `var.environment`, that variable must be defined in the child module's `variables.tf` file. The error 'Invalid reference' indicates Terraform cannot resolve the variable within the module's scope, meaning the child module does not declare an input variable named `environment`. Without this declaration, the module cannot accept the value passed from the root module.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The Terraform version does not support module variables

    Why it's wrong here

    Terraform has supported the concept of passing variables to child modules since its very early versions (e.g., 0.6+). This is a fundamental mechanism for abstracting and reusing configuration blocks, allowing modules to be parameterized. Therefore, an error indicating an undefined variable within a module context is highly unlikely to be caused by a lack of version support for module variables themselves. The issue typically lies with the *declaration* of the variable within the specific module's scope.

  • The variable `var.environment` is not defined in the root module's variables.tf

    Why it's wrong here

    While the root module *does* need to define `var.environment` if it intends to pass a value named `environment` to a child module, the error message described typically arises when the *child module itself* attempts to reference `var.environment` *internally* without having declared it. The root module's definition of `var.environment` is for its own scope and for providing input to the child, not for satisfying the child's internal variable declarations. The problem occurs when the child module tries to use an undeclared input.

  • The variable `environment` is not defined in the child module's variables.tf

    Why this is correct

    When a child module receives input values, it must explicitly declare those inputs as variables within its own scope, typically in a `variables.tf` file inside the module's directory. If the root module attempts to pass a value like `environment = var.environment` to a child module, the child module must have a corresponding `variable "environment" {}` block defined to accept that input. Without this declaration, the child module will report an error indicating an undefined variable when it tries to use `var.environment` internally, as it does not recognize `environment` as a valid input.

  • The module source path is invalid

    Why it's wrong here

    An invalid module source path, such as a typo in the local directory path or an unreachable remote repository, would typically result in a "Module not found" or "Failed to download module" error during the `terraform init` phase. This type of error occurs much earlier in the Terraform workflow, specifically during the module discovery and download process. It would not manifest as an undefined variable error during the plan or apply phase, which is when variable resolution issues usually become apparent.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

Courseiva writes every TF-004 question from scratch — 428 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.