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.
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.
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.
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.
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