- Variable block defaults
- Environment variables (
TF_VAR_prefix) - Terraform variable files (
.tfvars) - Command-line flags (
-var/-var-file)
| Method | Best for | Example |
|---|---|---|
Variable default | Safe baseline values, documentation | See variable block example below |
Environment variables (TF_VAR_) | Sensitive values from CI/CD/secret stores | export TF_VAR_db_password="..." |
.tfvars files | Grouping environment-specific settings | terraform plan -var-file="prod.tfvars" |
| Command-line flags | Ad-hoc overrides, testing | terraform apply -var="region=us-west-2" |
1) Variable defaults (variable block)
The simplest place to provide a value is inside thevariable block using default. Defaults serve as a fallback when no other input method supplies a value.
Example:
- Provide reasonable, non-sensitive baseline values for development or documentation.
- Use defaults to make modules easier to consume without requiring every caller to set every value.
- Never put secrets (API keys, passwords) in
default. - Keep defaults minimal and generic.
Do not store sensitive secrets (API keys, credentials, passwords) in variable
default values. Defaults are part of your configuration and may be committed to version control.2) Environment variables (TF_VAR_)
Terraform maps environment variables with theTF_VAR_ prefix to variables by name. This keeps secrets and environment-specific values out of repository files and integrates seamlessly with CI/CD secret stores.
Examples (bash):
- Keeps sensitive values out of code and
.tfvarsfiles. - Works well with CI/CD secrets and ephemeral runners.
- Simple to inject per session or per pipeline run.
- Environment variables are scoped to the session/runner and are not automatically versioned alongside your code.
Use environment variables for sensitive values that should not be checked into version control or for injecting secrets into CI/CD pipelines.
3) Terraform variable files (.tfvars)
.tfvars files are intentionally designed to hold variable assignments. Terraform automatically loads terraform.tfvars and any files ending in .auto.tfvars or .auto.tfvars.json in the working directory. Other .tfvars files—such as dev.tfvars, staging.tfvars, or prod.tfvars—must be passed explicitly with -var-file.

dev.tfvars:
.tfvars:
- Group variables by intent (network, VM, app, cloud account) and add comments for clarity.
- Use environment-specific files (
dev.tfvars,staging.tfvars,prod.tfvars) and pass the one you need at runtime. - Exclude
.tfvarsfiles from version control if they contain secrets (use.gitignore), or store non-secret environment defaults in the repo. - Use
terraform.tfvarsor*.auto.tfvarsfor values you want Terraform to pick up automatically in the current working directory.
.tfvars file:
4) Command-line flags (-var / -var-file)
Command-line flags provide the highest-priority overrides and are ideal for ephemeral changes, testing, or CI steps that must force a specific value.
- CLI
-varoverrides.tfvars, environment variables, and defaults. - Use
-var-fileon the command line to provide a tfvars file that differs from the auto-loaded files. - Avoid using
-varfor routine production config because command-line flags are not persisted in files and can be harder to audit.
Precedence: which source wins?
Terraform resolves variables using a strict order of precedence. When a variable is set in multiple places, the highest-priority source takes effect. Precedence from highest to lowest:- Command-line flags (
-varand-var-filespecified on the CLI) - Environment variables (
TF_VAR_prefixed) - Terraform variable files (auto-loaded
terraform.tfvars,*.auto.tfvars, and other*.tfvarswhen passed) - Variable block
defaultvalues

- If a variable has a
defaultand you set it indev.tfvars, thedev.tfvarsvalue wins over thedefault. - If you then set the same variable using
TF_VAR_VARNAME, the environment variable overridesdev.tfvars. - Finally, providing
-var="VAR=value"on the CLI will override the environment variable and all other sources.
Quick reference table for precedence
| Priority | Source | How to apply |
|---|---|---|
| 1 (highest) | Command-line flags | terraform apply -var="key=value" or -var-file="file.tfvars" |
| 2 | Environment variables | export TF_VAR_key="value" or pipeline secrets |
| 3 | .tfvars files | terraform.tfvars, *.auto.tfvars, or -var-file="file.tfvars" |
| 4 (lowest) | variable defaults | variable "key" { default = "value" } |
Summary and best practices
- Defaults: Use for safe, non-sensitive baselines and documentation.
- Environment variables: Use for secrets and CI/CD-injected values.
.tfvarsfiles: Use to group environment-specific settings; avoid committing secrets unless stripped.- Command-line flags: Use for ad-hoc overrides and testing; remember these are highest precedence.
- Always follow the precedence rules above to avoid unexpected overrides during runs.
- Terraform CLI docs: https://www.terraform.io/docs/cli
- Variable configuration: https://www.terraform.io/docs/language/values/variables.html