autotfvars
Auto.tfvars in Terraform: The Complete Guide to Automatic Variable Loading
Key Takeaways
- Auto.tfvars files are loaded automatically — Terraform reads
.auto.tfvarsand*.auto.tfvarsfiles without any command-line flags. - Sourcing order is critical — later files override earlier ones, and auto.tfvars values are overridden by explicit
-varand-var-fileflags. - They are not secret-safe by default — if your auto.tfvars contains sensitive values, ensure it is listed in
.gitignore. - Environment-specific naming is a best practice — files like
dev.auto.tfvars,staging.auto.tfvars, andprod.auto.tfvarskeep configurations clean and explicit. - AUTO_TFVARS environment variable exists — you can customize which auto.tfvars file Terraform loads via the
AUTO_TFVARSvariable.
What Is Auto.tfvars in Terraform?
In Terraform, an .auto.tfvars file is a special variables file that Terraform loads automatically during terraform plan and terraform apply — no -var-file flag required. This behavior applies to two patterns:
.auto.tfvars— a single auto-loaded file in the working directory.*.auto.tfvars— every file matching the pattern (alphabetical order applies).
These files contain the same HCL variable syntax as regular .tf files, but they exist solely to provide default values that override what is declared in your configuration without manual flag passing.
Why Do Auto.tfvars Files Exist?
Terraform was designed so that core infrastructure definitions live in version-controlled .tf files, while environment-specific overrides live separately. Auto.tfvars bridge that gap — they let you maintain a single codebase across dev, staging, and production without duplicating resources or using cumbersome inline variables.
How Auto.tfvars Works: The Sourcing Order
Understanding the order in which Terraform processes variable sources is essential. Values from later sources override earlier ones.
Complete Variable Sourcing Priority (Lowest to Highest)
- Variables declared directly in
.tffiles (withdefault =values) .auto.tfvars(single file, if present)*.auto.tfvars (all matching files, sorted alphabetically)-var-file=command-line flags (processed in the order given)-var="key=value"command-line flags- Environment variables (
TF_VAR_*) terraform.tfstatevalues (for outputs and state-dependent resolution)
Practical Example
# main.tf
variable "instance_type" {
default = "t2.micro"
}
variable "env" {
default = "dev"
}
# dev.auto.tfvars
instance_type = "t2.small"
env = "dev"
# staging.auto.tfvars
instance_type = "t2.medium"
env = "staging"
# terraform apply -var-file=prod.auto.tfvars
# (prod.auto.tfvars sets env = "prod" and instance_type = "t3.large")
# Result: env = "prod", instance_type = "t3.large"
Best Practices for Using Auto.tfvars
1. Use Environment-Specific Names
Naming convention <environment>.auto.tfvars is the industry standard. It makes intent obvious and prevents accidental cross-environment leakage.
2. Always Add Auto.tfvars to .gitignore
If your auto.tfvars contain sensitive data (API keys, passwords, internal IPs), never commit them. Add these patterns to your .gitignore:
*.auto.tfvars
.auto.tfvars
3. Pair with Terraform Cloud or Remote State
For team environments, store auto.tfvars values in Terraform Cloud workspaces or use a secrets manager rather than shipping raw files across the organization.
4. Keep a Template Auto.tfvars
Commit a file like auto.tfvars.example that documents which variables each environment needs, without exposing actual values.
5. Avoid the Single .auto.tfvars File at Scale
A lone .auto.tfvars file becomes unmanageable as environments grow. Use the wildcard pattern with explicit environment names instead.
Quick Checklist
| Practice | Priority | Impact |
|---|---|---|
| Name files by environment | High | Prevents config confusion |
| Add *.auto.tfvars to .gitignore | High | Prevents credential leaks |
| Use terraform.tfvars.example templates | Medium | Onboards new team members faster |
| Test with -var-file before relying on auto | Medium | Reduces plan surprises |
| Audit AUTO_TFVARS env var usage | Low | Avoids unexpected file selection |
Auto.tfvars vs. Terraform.tfvars vs. Explicit -var-file
| Feature | .auto.tfvars |
terraform.tfvars |
-var-file= (explicit) |
|---|---|---|---|
| Auto-loaded by Terraform | Yes | Yes | No — must specify flag |
| Multiple files supported | Yes (*.auto.tfvars) |