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.
# prod.tfvarsenvironment = "prod"aws_region = "ap-south-1"replica_count = 3terraform apply -var-file=prod.tfvarsexport TF_VAR_db_password="read-from-your-secret-store" # secrets via env varSecurityNever commit secrets in tfvars. Any
.auto.tfvarsfile loads automatically, so a committedsecrets.auto.tfvarsleaks 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. ===