Copyright (c) 2026 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

4.3.1. Input Variables

💡 First Principle: Input variables parameterize a configuration so the same code serves many situations — and because their values can come from several sources, Terraform follows a defined precedence so the result is predictable.

A variable block can declare a type, default, description, sensitive flag, nullable setting, and one or more validation blocks. Values can be supplied from multiple sources, and when the same variable is set more than once, later sources in this precedence win (lowest to highest):

Precedence (low → high)Source
1Environment variables (TF_VAR_name)
2terraform.tfvars
3terraform.tfvars.json
4*.auto.tfvars / *.auto.tfvars.json (alphabetical order)
5-var and -var-file on the command line (last one wins)

If no value is supplied and there's no default, Terraform prompts interactively (or errors in automation).

⚠️ Exam Trap: Command-line -var/-var-file has the highest precedence and overrides everything else; environment variables have the lowest. A frequent question gives a variable set in several places and asks which value wins — the answer is the command line.

Reflection Question: A variable is set in terraform.tfvars, via a TF_VAR_ environment variable, and with -var on the command line. Which value does Terraform use, and why is that ordering sensible?

Alvin Varughese
Written byAlvin Varughese
Founder18 professional certifications