TF-004 Read, generate and modify configuration Practice Question
A configuration uses variables defined in a 'variables.tf' file. The operator wants to override these variables for a specific run without modifying the file. Which method should they use?
⚠ Common exam trap
HashiCorp often tests the variable precedence order, and the trap here is that candidates confuse environment variables (`TF_VAR_*`) with the `-var` flag, thinking both are equally temporary, but environment variables persist for the entire shell session, whereas `-var` applies only to the single command invocation.
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 with 'terraform plan' or 'terraform apply'.
The `-var` flag on `terraform plan` or `terraform apply` allows operators to override variable values for a single run without modifying any files. This is the correct method because it provides a temporary override that does not persist across runs, unlike file-based or environment variable approaches.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Edit the state file directly.
Why it's wrong here
Directly editing the Terraform state file (`terraform.tfstate`) is highly discouraged and can lead to state corruption, drift, and unexpected infrastructure changes. The state file primarily records the real-world infrastructure managed by Terraform, mapping resources to their current attributes, not input variable definitions or their values. Therefore, attempting to modify variable values within the state file is ineffective and dangerous, as it's not where Terraform expects to find variable inputs for a run.
- ✗
Set environment variables with the same name as the variables.
Why it's wrong here
Setting environment variables with the exact same name as Terraform variables will not be recognized by Terraform as input values. For Terraform to correctly interpret an environment variable as a variable input, it must be prefixed with `TF_VAR_`. Without this specific prefix, Terraform ignores the environment variable, meaning the intended variable value will not be passed to the configuration during `plan` or `apply` operations.
- ✗
Create a 'terraform.tfvars' file.
Why it's wrong here
Creating a `terraform.tfvars` file (or `terraform.tfvars.json`) is a standard method for providing variable values, but it's designed for persistent, default values that apply to all subsequent Terraform runs in that directory. While effective for general configuration, it's not the appropriate mechanism for a *single-run* override, as its contents would continue to be applied in future operations unless manually removed or modified, which contradicts the requirement for a temporary, one-time value.
- ✓
Use the '-var' flag with 'terraform plan' or 'terraform apply'.
Why this is correct
The `-var` flag, used with commands like `terraform plan` or `terraform apply`, is the most direct and idiomatic way to provide or override variable values for a single Terraform execution. Values supplied via `-var` on the command line take the highest precedence, ensuring they are used for that specific run, and are not persisted for subsequent operations. This method is ideal for temporary adjustments, sensitive data, or testing different configurations without modifying files.
Go deeper
Related to this question
About these practice questions
This TF-004 question is part of Courseiva's 428-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.