Terraform cannot read your terraform.tfvars file due to syntax errors, missing variable declarations, file naming issues, or incorrect file paths. Fix the tfvars file syntax and ensure variables are declared in your .tf files.
Terraform loads variables from terraform.tfvars files, but this error occurs when the file cannot be parsed or accessed. This typically happens when: 1. The .tfvars file has syntax errors (invalid HCL) 2. Variables are defined in terraform.tfvars but not declared in .tf files 3. The file is named incorrectly (e.g., terraform.tfvar instead of terraform.tfvars) 4. The file path is wrong or the file doesn't exist 5. Terraform is being run from the wrong working directory Unlike other tools, Terraform requires that every variable used in .tfvars must be explicitly declared in a .tf file first before you can assign values to it.
Ensure the file is named exactly terraform.tfvars (not terraform.tfvar or terraform.tfvars.json unless you're specifically using JSON format).
# List files in current directory
ls -la terraform.tfvars
# Or from parent directory
ls -la your-project/terraform.tfvarsThe file must be in the root module directory where you run terraform commands. If it's in a subdirectory, you'll need to use the -var-file flag.
Check for HCL syntax errors in terraform.tfvars. Common mistakes include:
- Missing quotes around string values: instance_type = t2.micro should be instance_type = "t2.micro"
- Incorrect object/map syntax: tags = { Name = "test" } (must use =)
- Missing commas between properties (not required in HCL but object syntax must be correct)
# Correct syntax examples
region = "us-east-1"
instance_count = 3
enable_feature = true
tags = { Name = "web-server", Env = "prod" }
availability_zones = ["us-east-1a", "us-east-1b"]Review your file character by character for these issues.
Every variable used in terraform.tfvars must be declared in a .tf file (typically variables.tf). Variable declarations define what variables Terraform expects.
# variables.tf
variable "region" {
type = string
description = "AWS region"
default = "us-east-1"
}
variable "instance_count" {
type = number
default = 1
}
variable "tags" {
type = map(string)
}If terraform.tfvars contains a variable that doesn't have a corresponding declaration, Terraform will fail. Add the missing declarations.
Terraform must be run from the directory containing your root module .tf files. If terraform.tfvars is in a different location, use the -var-file flag.
# If terraform.tfvars is in current directory (default behavior)
terraform plan
# If terraform.tfvars is elsewhere
terraform plan -var-file="./environments/prod.tfvars"
# Multiple .tfvars files
terraform plan -var-file="./common.tfvars" -var-file="./prod.tfvars"The auto-loading of terraform.tfvars only works when the file is in the same directory as your .tf files.
terraform validate will often provide more specific error details about what's wrong with your configuration and .tfvars files.
terraform validateThis command checks syntax without making any cloud API calls, so it's safe to run repeatedly. It will tell you exactly which line or variable is causing the problem.
Auto-loading behavior: Terraform automatically loads files named terraform.tfvars or matching the pattern *.auto.tfvars. Files matching *.auto.tfvars are loaded in lexical order. Any other filename requires the -var-file flag.
Variable precedence: If the same variable is set in multiple .tfvars files, the last one loaded wins. The precedence order is: environment variables (TF_VAR_*) → terraform.tfvars → *.auto.tfvars → command-line (-var) → variable defaults.
Terraform Cloud/Enterprise: If using TFE/Cloud with a configured working directory, ensure that working directory setting in the project settings matches where your .tf and .tfvars files are located.
Environment variables: You can also set variables using TF_VAR_variablename environment variables as an alternative to .tfvars files.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform