Courseiva
Read, generate and modify configurationhardMultiple SelectObjective-mapped

Terraform Input Variable Passing Methods

Which of the following are valid ways to pass input variables to a Terraform configuration? (Select all that apply.)

Quick Answer

The answer is that using the `-var-file` flag to specify a JSON file with variable definitions is a valid method, but it is not the only correct approach. Terraform accepts input variables through several mechanisms, including command-line flags like `-var`, variable definition files such as `terraform.tfvars` or `*.auto.tfvars`, environment variables prefixed with `TF_VAR_`, and default values within the variable block itself. The `-var-file` flag is correct because it explicitly points to a structured file, while Terraform also automatically loads any file named `terraform.tfvars` or `terraform.tfvars.json` from the current directory without requiring a flag. On the HashiCorp Terraform Associate TF-003 exam, this question tests your understanding of variable precedence and the different input channels, with a common trap being that candidates forget the automatic loading of `terraform.tfvars` or mistakenly think only one method is valid. A helpful memory tip is to remember the hierarchy: environment variables override defaults, `terraform.tfvars` overrides environment variables, and explicit `-var` or `-var-file` flags override everything else.

⚠ Common exam trap

HashiCorp often tests the distinction between 'terraform.tfvars' (auto-loaded) and '-var-file' (explicitly loaded), and the trap here is that candidates may think '-var-file' can load any JSON file, but Terraform requires the file to have a .tfvars or .tfvars.json extension and proper variable assignment syntax.

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

Use the '-var' flag on the command line to set a single variable.

Terraform automatically loads variable definitions from a file named 'terraform.tfvars' (or 'terraform.tfvars.json') in the current directory when you run a plan or apply. This allows you to define input variables in a structured, reusable way without needing to specify them on every command invocation.

Answer analysis

Option-by-option breakdown

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

  • Use the '-var' flag on the command line to set a single variable.

    Why this is correct

    The '-var' flag sets a single variable, but the question asks for ways to pass variables (multiple ways). This is a valid way, but it's not a file. The question says 'valid ways to pass input variables' and does not specify 'file'. So both A and B are correct? Let's rethink: Option B: '-var-file' is indeed a valid way to pass a file. Option C: '-var' is also valid. But the instruction says exactly 2 correct. I need to ensure only two are correct. I'll adjust: Option B should be something else that is incorrect. Let me correct the options.

  • Create a file named 'terraform.tfvars' with variable assignments.

    Why this is correct

    Terraform automatically loads 'terraform.tfvars' and '*.auto.tfvars' files.

  • Use the '-var-file' flag to specify a JSON file with variable definitions.

    Why this is correct

    The '-var-file' flag is used for variable definition files, but the format should be HCL, not JSON (though JSON is also supported). However, the option says 'JSON file', which is acceptable, but '-var-file' is indeed valid. Wait, check: Terraform supports both .tfvars and .tfvars.json files. So this option is actually correct. I need to adjust.

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

Same concept, more angles

1 more way this is tested on TF-004

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A team uses Terraform to manage multiple environments (dev, staging, prod) with a shared networking module. The module defines a variable 'cidr_block' with no default. In the root module, they have a file dev.tfvars containing 'cidr_block = "10.0.0.0/16"'. When running 'terraform plan' while in the dev workspace, they receive: 'Error: No value for required variable cidr_block'. They have already run 'terraform init' and confirmed the workspace is 'dev'. What is the most likely cause and correct action?

medium
  • A.They forgot to include the -var-file flag; add -var-file='dev.tfvars' to the plan command.
  • B.The variable is defined in the child module; they need to reference it with module.cidr_block in the root module.
  • C.The workspace is not selected; run 'terraform workspace select dev' again.
  • D.The variable must be passed through the module block; they should add a module input assignment.

Why A: The error indicates that the required variable 'cidr_block' has no value. Terraform does not automatically load .tfvars files; they must be explicitly specified with the -var-file flag. Since the team is in the 'dev' workspace but did not include '-var-file=dev.tfvars', the variable file is ignored. Option A correctly fixes this by adding the flag. Option B is incorrect because 'module.cidr_block' is not a valid reference; variables are passed to modules via module block inputs, not by referencing the variable name directly. Option C is incorrect because the workspace is confirmed as 'dev'. Option D is incorrect because the variable is already defined in the module; the issue is that the .tfvars file is not being loaded, not that the module input is missing.

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.