Skip to main content

tfvars File

A tfvars file assigns values to Terraform input variables, separate from the variable declarations. Files named terraform.tfvars or ending in .auto.tfvars load automatically, and others load with the -var-file flag.

What Is a tfvars File?

variables.tf declares inputs, and a tfvars file fills in values per environment. Values are literals, so you cannot reference var.x inside.

HCL
# prod.tfvars
environment = "prod"
aws_region = "ap-south-1"
replica_count = 3
Bash
terraform apply -var-file=prod.tfvars
export TF_VAR_db_password="read-from-your-secret-store" # secrets via env var
Security

Never commit secrets in tfvars. Any .auto.tfvars file loads automatically, so a committed secrets.auto.tfvars leaks credentials into Git history.

Frequently Asked Questions

What is the precedence when several sources set a variable?

Defaults are lowest, then environment variables, terraform.tfvars, auto.tfvars files in alphabetical order, and finally -var and -var-file flags in command-line order.

Can a tfvars file use expressions?

No. Only literal values are allowed. Compute derived values with a local value in your configuration. ===

Found a mistake, an outdated step or a broken link?