How to fix Warning: Value for undeclared variable in Terraform
The "Value for undeclared variable" warning occurs when you provide a value for a variable that has not been declared in your Terraform configuration. Fix it by adding a variable block to declare the variable or removing the unused value assignment.
Quick fix
Fixes Warning: Value for undeclared variable
variable "your_variable_name" {
type = string
description = "Description of what this variable is for"
}- Declare the missing variable
- Match variable names exactly
- Check variable file locations
- Specify variable type and default
variable "your_variable_name" {type = stringdescription = "Description of what this variable is for"}Warning: Value for undeclared variable
What this error means
This warning appears when Terraform detects that you are trying to assign a value to a variable that has not been explicitly declared in your root module configuration. Terraform requires all variables to be declared with a "variable" block before they can be used. This validation exists to catch typos in variable names, prevent accidental configuration mismatches, and ensure that your configuration is maintainable. When you provide a value for an undeclared variable (either via terraform.tfvars, .auto.tfvars, or command-line -var flags), Terraform warns you that it cannot process this value because there is no corresponding variable declaration to define its type, default value, or validation rules. While the warning doesn't immediately block operations, it indicates a misconfiguration that should be addressed. In future Terraform versions, this warning may become an error.
Typical symptoms
- Terraform plan or apply shows "Warning: Value for undeclared variable" message
- Error message displays the name of the undeclared variable
- Values in terraform.tfvars are ignored without corresponding variable declarations
- Environment variables with TF_VAR_ prefix for undeclared variables are silently ignored
- Variables defined in .auto.tfvars files are not recognized
- Terraform succeeds but with warnings appearing in output
Common causes
- Variable block is missing from variables.tf or other .tf file
- Typo in the variable name between the declaration and assignment
- Variable is defined in a module but being provided at root level
- Copy-pasting terraform.tfvars from another project without updating variable declarations
- Collaborative teams where one person provides values without another declaring them
- Common shared terraform.tfvars file used across multiple modules with different variable sets
How to fix "Warning: Value for undeclared variable"
Add a variable block to declare the variable in your Terraform configuration. Create a variables.tf file or add to an existing one:
variable "your_variable_name" {
type = string
description = "Description of what this variable is for"
}Replace "your_variable_name" with the actual variable name from your warning message. The variable block tells Terraform about the variable's type and purpose.
Ensure the variable name in your terraform.tfvars or -var flag exactly matches the declared variable name, including case sensitivity:
# In variables.tf
variable "instance_type" {
type = string
}
# In terraform.tfvars - must match exactly
instance_type = "t3.micro"Variable names are case-sensitive. "instance_type" and "InstanceType" are treated as different variables.
Ensure your variable definitions and value assignments are in the correct locations:
- Root module variables: Declare in variables.tf in the root directory
- Module-specific variables: Declare in variables.tf within the module directory
- Values for root variables: Provide in terraform.tfvars or root module via -var
- Values for module variables: Pass through the module block in parent configuration
# Root variables.tf
variable "environment" {
type = string
}
# Module call in main.tf
module "example" {
source = "./modules/example"
# This passes the value to module's declared variable
}Make your variable declaration complete with type and optional default value:
variable "region" {
type = string
description = "AWS region for deployment"
default = "us-east-1"
}
variable "tags" {
type = map(string)
description = "Common tags for all resources"
default = {
Environment = "production"
ManagedBy = "terraform"
}
}Specifying type prevents type mismatches and adds validation.
If the variable is not actually needed, remove the assignment from your terraform.tfvars or command line:
# Remove from terraform.tfvars
# unused_variable = "value"
# Or remove from command line
# terraform apply -var="unused_variable=value" # Delete this flag
terraform applyOnly provide values for variables you actually use in your configuration.
If you want to see warnings but with less verbosity during troubleshooting:
terraform plan -compact-warnings
terraform apply -compact-warningsThis reduces warning output clutter but still shows you the undeclared variable warnings.
Advanced notes
In Terraform Cloud and HCP Terraform, variable values set via the UI or API are automatically recognized without this warning, but values from variable definition files still require declarations. For monorepo setups with multiple modules, use variable validation blocks to ensure consistency:
variable "environment" {
type = string
description = "Deployment environment"
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Environment must be dev, staging, or prod."
}
}When using multiple .tfvars files (e.g., terraform.tfvars and prod.tfvars), remember that all variable assignments must have corresponding variable blocks in any .tf file. The -var-file flag applies values from additional files, but still requires the variables to be declared.
For collaborative teams, document all expected variables in a VARIABLES.md or include variable descriptions directly in the Terraform code. This prevents the common issue where someone adds a value to the shared terraform.tfvars without realizing other team members haven't declared it yet.
Frequently asked questions
- What does "Warning: Value for undeclared variable" mean?
- This warning appears when Terraform detects that you are trying to assign a value to a variable that has not been explicitly declared in your root module configuration. Terraform requires all variables to be declared with a "variable" block before they can be used. This validation exists to catch typos in variable names, prevent accidental configuration mismatches, and ensure that your configuration is maintainable.
- How do I fix "Warning: Value for undeclared variable"?
- The "Value for undeclared variable" warning occurs when you provide a value for a variable that has not been declared in your Terraform configuration. Fix it by adding a variable block to declare the variable or removing the unused value assignment.
- What causes "Warning: Value for undeclared variable"?
- Variable block is missing from variables.tf or other .tf file Typo in the variable name between the declaration and assignment Variable is defined in a module but being provided at root level
- Is the "Warning: Value for undeclared variable" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.