A Terraform variable file stores input variable values separately from configuration, reducing repeated typing and making environment-specific deployments easier to reproduce. It is especially useful when interactive input would create errors or cannot support automation. section 5.4.1 section 5.4
HCL variable files use simple key = value expressions rather than blocks and may contain literal values of any supported data type, including objects and maps. JSON is also supported, particularly when another system generates the file. section 5.4.3
Load environment values
Store production values in production.tfvars, then run terraform plan -var-file=production.tfvars. Terraform also loads terraform.tfvars, terraform.tfvars.json, and matching *.auto.tfvars or *.auto.tfvars.json files automatically when they exist. section 5.4.3
Listing 5.6 Variable file with HCL
# Production Data #1 vpc = "vpc-01234567890abcdef" #2 num_instances = 2
An HCL variable-file example assigns a VPC identifier and instance count with readable comments. section 5.4.3
Listing 5.7 Variable file with JSON
{ #1
"vpc": "vpc-01234567890abcdef", #2
"num_instances": 2 #3
}
A JSON variable-file example represents the same kind of input values for generated or programmatically managed files. section 5.4.3
Choose the file form and loading method deliberately:
- Use .tfvars for human-managed HCL and .tfvars.json for JSON; Terraform uses the extension to determine the format. section 5.4.3
- Pass -var-file multiple times when combining files, or use automatic naming patterns for standard inputs. section 5.4.3
- Treat sensitive values cautiously: variable files may enter version control, while CI/CD secret support or a secrets manager can keep Terraform from handling the secret directly. section 5.4.6

