How to Use -var-file in Terraform for Variable Management
Use
-var-file with the terraform apply or terraform plan commands to load variables from a file instead of the command line. This helps keep your variable values organized and reusable by specifying a file like terraform apply -var-file="vars.tfvars".Syntax
The -var-file option tells Terraform to load variables from a specified file. This file contains variable definitions in HCL or JSON format. You can use it with commands like terraform plan or terraform apply.
Syntax parts:
-var-file="filename": The path to your variable file.terraform planorterraform apply: Terraform commands that accept the variable file.
bash
terraform apply -var-file="variables.tfvars"
Example
This example shows how to define variables in a file and use -var-file to apply them.
1. Create a file named variables.tfvars with variable values.
2. Use terraform apply -var-file="variables.tfvars" to apply the configuration with those values.
hcl
/* variables.tfvars */ region = "us-west-2" instance_type = "t2.micro" /* main.tf */ variable "region" { description = "AWS region" type = string } variable "instance_type" { description = "EC2 instance type" type = string default = "t2.small" } provider "aws" { region = var.region } resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = var.instance_type } /* Command to run */ terraform init terraform apply -var-file="variables.tfvars" -auto-approve
Output
aws_instance.example: Creating...
aws_instance.example: Creation complete after 10s [id=i-0abcd1234efgh5678]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Common Pitfalls
Common mistakes when using -var-file include:
- Using the wrong file path or filename causes Terraform to ignore the file.
- Incorrect variable names or types in the file lead to errors.
- Forgetting to include
-var-filemeans default or command-line variables are used instead. - Using multiple
-var-fileoptions can override variables; the last file has priority.
bash
/* Wrong usage example: missing quotes or wrong path */ terraform apply -var-file=variables.tfvars /* Correct usage example: quotes and correct path */ terraform apply -var-file="variables.tfvars"
Quick Reference
| Option | Description | Example |
|---|---|---|
| -var-file | Load variables from a file | terraform apply -var-file="vars.tfvars" |
| Multiple -var-file | Use multiple files; last overrides | terraform apply -var-file="file1.tfvars" -var-file="file2.tfvars" |
| Variable file format | HCL or JSON format supported | region = "us-west-2" |
| Default variable file | Terraform automatically loads terraform.tfvars | No need to specify -var-file for terraform.tfvars |
Key Takeaways
Use -var-file to load variables from a file for cleaner Terraform commands.
Variable files must be in valid HCL or JSON format with correct variable names.
Specify the correct path and filename with quotes to avoid errors.
Multiple -var-file options can be used; later files override earlier ones.
Terraform automatically loads terraform.tfvars without needing -var-file.