terraformtfvars
Terraform .tfvars Files: A Practical Playbook for Managing Variables
Mastering .tfvars files is essential for writing safe, reproducible, and scalable Terraform code. This playbook provides actionable steps, checklists, and best practices to manage your infrastructure-as-code variables effectively.
Key Takeaways
- Separation of Concerns: Keep your main
.tffiles clean by defining variables once and managing their values in separate.tfvarsfiles. - Default Behavior: If you run
terraform applywithout a specific flag, Terraform automatically loadsterraform.tfvarsandterraform.auto.tfvars. All other.tfvarsfiles must be explicitly specified using the-var-fileflag. - Precedence Matters: Command-line flags (
-var) override variable files. Variable files override defaults defined invariableblocks. - Security First: Never commit sensitive secrets (API keys, passwords) to version control. Use encrypted sources or Terraform Cloud for sensitive data.
File Naming Conventions and Precedence
Understanding which files Terraform loads, and in what order, prevents unexpected overwrites. The order of precedence from lowest to highest is as follows:
- Lowest Precedence:
terraform.tfvarsandterraform.auto.tfvars(loaded automatically). - Medium Precedence: Any file passed explicitly via the
-var-fileflag. - Highest Precedence: Values passed via the
-varflag on the command line.
Comparison Table: Variable Source Types
| Source | File Name | Auto-Loaded? | Use Case |
|---|---|---|---|
| Default | terraform.tfvars |
Yes | Project-wide defaults, non-sensitive config. |
| Default (Hidden) | terraform.auto.tfvars |
Yes | Values that should not be checked into VCS (ignored by git by default in some conventions). |
| Environment Specific | dev.tfvars, prod.tfvars |
No | Environment-specific configurations loaded via -var-file. |
| CI/CD Specific | ci.tfvars |
No | Pipeline-specific variables, often generated dynamically. |
Actionable Checklist: Implementing .tfvars Best Practices
- Define Variables in Code: Ensure every variable used in your
.tffiles is declared in avariableblock with a description and, ideally, a default value. - Create a Base File: Create
terraform.tfvarsfor shared, non-sensitive values (e.g., instance types, region). - Split by Environment: Create
dev.tfvarsandprod.tfvarsfor values that change per environment (e.g., instance sizes, tags). - Use Explicit Flags: When applying, always specify the environment file:
terraform apply -var-file="prod.tfvars". - Handle Secrets Separately: Do not put secrets in
.tfvarsfiles that are committed to Git. Useterraform.auto.tfvars(if ignoring it in Git) or an external secret manager. - Validate Syntax: Run
terraform validateafter updating.tfvarsfiles to catch syntax errors early.
Common Pitfalls and How to Avoid Them
Accidentally Overwriting Critical Values
If you have a sensitive value in terraform.tfvars and pass a less secure value via -var-file=dev.tfvars, you might think the dev file wins. Remember: terraform.tfvars is loaded first. If a variable is defined in both, the later file in the load sequence (or command-line flag) overrides the earlier one. To avoid confusion, keep terraform.tfvars minimal.
Forgetting the -var-file Flag
Running terraform apply in a prod environment directory but forgetting -var-file=prod.tfvars will use the defaults from terraform.tfvars. This can deploy incorrect infrastructure. Mitigate this by using terraform workspace or separate directories per environment.
FAQs
Q1: Can I use .tfvars files for secrets?
No. Standard .tfvars files are plain text and often committed to version control. For secrets, use terraform.auto.tfvars (and add it to .gitignore), or better yet, use Terraform Cloud/Enterprise, HashiCorp Vault, or AWS Secrets Manager. Never commit secrets to your repository.
Q2: What is the difference between terraform.tfvars and variables.tf?
variables.tf is where you declare variables, their types, descriptions, and default values. terraform.tfvars is where you assign actual values to those declared variables. You need both: the declaration defines the contract, and the .tfvars file provides the implementation data.
Q3: How do I load multiple .tfvars files at once?
You can specify multiple -var-file flags in a single command. For example: terraform apply -var-file=base.tfvars -var-file=dev.tfvars. The files are loaded in the order specified. If a variable is defined in both base.tfvars and dev.tfvars, the value from dev.tfvars will take precedence because it is listed last.