How to Use terraform.tfvars for Variable Configuration
Use a
terraform.tfvars file to set values for variables declared in your Terraform configuration. Terraform automatically loads this file to apply variable values without needing to specify them on the command line.Syntax
The terraform.tfvars file contains variable assignments in key = value format. Each key matches a variable name declared in your Terraform files. Values can be strings, numbers, booleans, lists, or maps.
This file should be placed in the same directory as your Terraform configuration files. Terraform automatically loads it when you run commands like terraform apply.
terraform
variable "region" { description = "AWS region" type = string } variable "instance_count" { description = "Number of instances" type = number } # terraform.tfvars region = "us-west-2" instance_count = 3
Example
This example shows how to declare variables in Terraform and set their values using terraform.tfvars. When you run terraform apply, Terraform reads the values from the terraform.tfvars file automatically.
terraform
# main.tf variable "region" { description = "AWS region" type = string } variable "instance_count" { description = "Number of instances" type = number } provider "aws" { region = var.region } resource "aws_instance" "example" { count = var.instance_count ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" } # terraform.tfvars region = "us-west-2" instance_count = 2
Output
Terraform will create 2 AWS EC2 instances in the us-west-2 region using the specified AMI and instance type.
Common Pitfalls
- File name typo: The file must be named exactly
terraform.tfvarsorterraform.tfvars.jsonto be auto-loaded. - Variable name mismatch: Keys in
terraform.tfvarsmust match variable names declared in Terraform files. - Wrong value types: Values must match the declared variable types (string, number, list, etc.).
- Multiple tfvars files: If you use multiple
.tfvarsfiles, onlyterraform.tfvarsis auto-loaded; others require explicit loading with-var-file.
terraform
/* Wrong: variable name mismatch */ # terraform.tfvars region_wrong = "us-west-2" /* Correct: matching variable name */ region = "us-west-2"
Quick Reference
Tips for using terraform.tfvars:
- Place
terraform.tfvarsin the root of your Terraform project. - Use
key = valuepairs matching your variable names. - For sensitive values, consider using environment variables or
terraform.tfvars.jsonwith encryption. - To override
terraform.tfvars, use-varor-var-fileoptions.
Key Takeaways
terraform.tfvars sets variable values automatically when placed in the Terraform project directory.
Variable names in terraform.tfvars must exactly match those declared in Terraform configuration files.
Values in terraform.tfvars must match the expected variable types to avoid errors.
Only terraform.tfvars and terraform.tfvars.json are auto-loaded; other files need explicit loading.
Use terraform.tfvars for cleaner variable management instead of passing variables via command line.