Courseiva
Read, generate and modify configurationmediumMultiple SelectObjective-mapped

Terraform Input Variables — Declaration and Assignment Methods

Which TWO statements about Terraform's handling of input variables are true?

Quick Answer

The correct answer is that variables can be declared in a .tf file using the 'variable' block, and Terraform automatically loads a terraform.tfvars file for assignment. This is because the variable block defines the input variable's name, type, and optional default, while Terraform's default variable file, terraform.tfvars, is loaded automatically to supply values without requiring explicit -var-file flags. On the HashiCorp Terraform Associate TF-003 exam, this concept tests your understanding of declaration versus assignment methods, often appearing in questions that try to confuse you with the requirement that variables must be assigned before plan or that defaults are set in .tfvars files. A common trap is assuming environment variables with the TF_VAR_ prefix cannot override .tfvars values, but they can. Remember the memory tip: "Declare with variable, assign with tfvars, override with TF_VAR."

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 terraform.tfvars file is automatically loaded by Terraform.

Options C and E are correct. Option C is correct because Terraform automatically loads terraform.tfvars and any *.auto.tfvars files in the working directory. Option E is correct because variables are declared using the 'variable' block in .tf files. Option A is incorrect: variables can have default values and are not required to be assigned before terraform plan. Option B is incorrect: variable values can be overridden using environment variables with the TF_VAR_ prefix. Option D is incorrect: default values are set within the variable block using the default argument, not in .tfvars files.

Answer analysis

Option-by-option breakdown

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

  • All variables must be assigned a value before running terraform plan.

    Why it's wrong here

    Variables can have default values, making assignment optional. If a default is present, it is used.

  • Variable values cannot be overridden using environment variables.

    Why it's wrong here

    Environment variables with the TF_VAR_ prefix can override variable values.

  • The terraform.tfvars file is automatically loaded by Terraform.

    Why this is correct

    Terraform automatically loads terraform.tfvars and any .auto.tfvars files in the root module directory.

  • Default values for variables can only be set using a .tfvars file.

    Why it's wrong here

    Default values are set within the variable block in .tf files, not in .tfvars files.

  • Variables can be declared in a .tf file using the 'variable' block.

    Why this is correct

    The 'variable' block is the standard way to declare input variables in Terraform configuration.

About these practice questions

One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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 statements about Terraform configuration files are correct? (Choose two.)

easy
  • A.All .tf files in subdirectories are automatically loaded.
  • B.A file named terraform.tfvars is automatically processed.
  • C.The backend configuration must be defined in the same file as the provider configuration.
  • D.The -var-file flag accepts a comma-separated list of variable files.
  • E.Variable definitions files can have .tfvars.json extension.

Why B: Options B and E are correct. Terraform automatically processes a file named terraform.tfvars if it exists in the configuration directory, making option B correct. Variable definitions files can also have a .tfvars.json extension, as stated in option E. Option A is incorrect because Terraform does not load .tf files from subdirectories by default; only the root directory is scanned. Option C is incorrect because the backend configuration does not have to be defined in the same file as the provider configuration; they can be in separate files. Option D is incorrect because the -var-file flag does not accept a comma-separated list; you must use multiple -var-file flags to specify multiple variable files.

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.