Courseiva
Understand Terraform basics →mediumMultiple Select

Three Ways to Pass Variables to Terraform

Which three of the following are valid ways to pass variable values to a Terraform configuration?

Quick Answer

The answer is three valid ways to pass variables to a Terraform configuration: using a .tfvars file, environment variables with the TF_VAR_ prefix, and the -var flag on the command line. These methods work because Terraform’s variable resolution follows a clear precedence order—command-line flags override .tfvars files, which in turn override environment variables—allowing you to inject values without hardcoding them in your configuration. On the HashiCorp Terraform Associate TF-003 exam, this question tests your understanding of variable assignment mechanics, and a common trap is confusing the -var-file flag (which is also valid but not listed here) with the unsupported -assign flag or YAML files. To remember the three core methods, think of the mnemonic “F-E-V” for File, Environment, and Variable flag—just don’t forget the TF_VAR_ prefix for environment variables.

⚠ Common exam trap

Terraform certification exams often test the distinction between valid Terraform variable assignment methods and fictitious or unsupported formats, so candidates must remember that only `-var`, `.tfvars` files, and `TF_VAR_` environment variables are officially supported, while YAML files and non-existent flags like `-assign` are common distractors.

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

✓

Using the -var flag on the command line.

Option B is correct because Terraform's CLI accepts variable values directly with the -var flag, e.g. terraform apply -var="region=us-east-1", which overrides the variable's default for that run. Option D is correct because Terraform automatically loads variable values from files ending in .tfvars or .tfvars.json (such as terraform.tfvars or a file passed via -var-file), making them a standard way to supply input variables. Option E is correct because Terraform reads environment variables named with the TF_VAR_ prefix, so TF_VAR_region=us-east-1 sets the value of a declared variable named region. Option A is not valid because Terraform has no -assign flag; the correct CLI flag is -var. Option C is not valid because Terraform does not natively parse YAML files for input variables — variable files must be HCL .tfvars or JSON .tfvars.json.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Using the -assign flag on the command line.

    Why it's wrong here

    Terraform's command-line variable flag is -var (with -var-file for files); -assign does not exist, so the command fails. It is tempting because passing values at apply time is legitimate, and -var would be correct when overriding a declared variable for a single plan or apply run.

  • ✓

    Using the -var flag on the command line.

    Why this is correct

    The `-var` flag supplies a value directly at invocation, such as `terraform apply -var="region=westeurope"`, satisfying the requirement for a valid input method. It overrides defaults for root-module input variables without editing files, making it ideal for one-off or scripted runs.

  • ✗

    Using a YAML file.

    Why it's wrong here

    Terraform loads variable values from .tfvars, .tfvars.json, environment variables (TF_VAR_), CLI -var flags and auto-loaded files; it has no YAML variable-file parser. YAML suits Ansible inventories or Kubernetes manifests, so it tempts anyone mixing tooling, but Terraform cannot read it here.

  • ✓

    Using a .tfvars file.

    Why this is correct

    A `.tfvars` file supplies values for declared input variables, which Terraform automatically loads when named `terraform.tfvars` or `*.auto.tfvars`, or when passed explicitly via `-var-file`. This satisfies the stem's requirement for a valid mechanism to pass variable values into a configuration without editing the configuration itself.

  • ✓

    Using environment variables with the TF_VAR_ prefix.

    Why this is correct

    Terraform automatically reads any environment variable prefixed with `TF_VAR_` and maps it to the matching input variable, so `TF_VAR_region=eu-west-1` sets `var.region` without editing files. This satisfies the scenario's requirement for a valid input method, alongside CLI flags and `.tfvars` files.

About these practice questions

This TF-004 question is part of Courseiva's 434-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 →

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. Which TWO of the following are valid ways to pass variable values to a Terraform configuration? (Choose two.)

hard
  • ✓ A.Use a `.tfvars` file and run `terraform apply`
  • B.Use a `.tfvars.json` file
  • C.Add variable values in the `variables.tf` file
  • D.Create a `.env` file in the working directory
  • ✓ E.Use the `-var` flag on the command line

Why A: Option A is correct because Terraform automatically loads variable values from files named terraform.tfvars or files matching *.auto.tfvars in the working directory when you run terraform apply, and you can also pass any .tfvars file explicitly with -var-file. Option E is correct because the -var flag lets you set individual input variable values directly on the command line, e.g. terraform apply -var="region=us-east-1", which overrides values from other sources. Option B is not a valid mechanism: Terraform does not automatically load .tfvars.json files, and JSON variable files must be passed explicitly via -var-file (e.g. terraform apply -var-file=values.tfvars.json). Option C is wrong because variables.tf is used to declare input variables with variable blocks, not to assign values to them. Option D is wrong because Terraform has no support for .env files; environment variables must be prefixed with TF_VAR_ (e.g. TF_VAR_region) to be read as input variable values.

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.