This Terraform error occurs when your configuration references a variable using var.variable_name syntax, but the variable hasn't been declared. Fix it by adding the missing variable block to your Terraform code.
The "Reference to undeclared input variable" error in Terraform indicates that you're trying to use a variable (via the var.variable_name syntax) that hasn't been defined in your configuration. Terraform requires all variables to be explicitly declared before they can be referenced. In Terraform, variables are the input interface for your modules and root configuration. They allow users to pass custom values at runtime without modifying the code. When Terraform encounters a reference to a variable that doesn't exist, it stops execution with this error to prevent misconfigurations. This error commonly occurs in three scenarios: when you forget to declare a variable altogether, when you have a typo in the variable name between declaration and usage, or when using modules where each module has its own variable scope.
Read the error message carefully to see which variable is undeclared. The error will show the exact variable name that's missing:
Error: Reference to undeclared input variable
on main.tf line 5, in resource "aws_instance" "example":
5: region = var.aws_region
^^^^^^^^^^^^^^
An input variable with the name "aws_region" has not been declared. This variable can be declared with a variable block.In this example, the variable aws_region is missing.
Create a new variable block in your Terraform configuration. The standard practice is to create a variables.tf file in the same directory:
# variables.tf
variable "aws_region" {
description = "The AWS region to deploy resources"
type = string
}Or add the variable to any .tf file in your configuration directory:
variable "aws_region" {
description = "The AWS region where resources will be created"
type = string
default = "us-east-1" # Optional default value
}The variable block requires at minimum:
- Variable name (matches the identifier in var.name)
- Optionally: description, type, default, validation blocks
If you already have a variable declared but still get the error, check for spelling differences. Variable names are case-sensitive:
Incorrect - typo in the usage:
variable "my_variable" {
type = string
}
resource "aws_instance" "example" {
# Typo: my_variabl is missing the 'e'
tags = {
Name = var.my_variabl
}
}Correct:
variable "my_variable" {
type = string
}
resource "aws_instance" "example" {
tags = {
Name = var.my_variable
}
}Use grep or search to verify consistency:
# Check all occurrences of the variable
grep -r "my_variable" .When using modules, variables must be declared in the module's own directory, not in the root module. Each module has its own variable namespace.
Root module (main.tf):
module "networking" {
source = "./modules/networking"
cidr_block = var.vpc_cidr # References root module variable
}
variable "vpc_cidr" {
type = string
}Module (modules/networking/main.tf):
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr # ERROR - this variable is not in this module's scope
}Module (modules/networking/variables.tf):
variable "vpc_cidr" {
type = string
}Module (modules/networking/main.tf) - Corrected:
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr # Now this works because vpc_cidr is declared in this module
}When using modules from the Terraform Registry, check the module's documentation for required input variables. Create matching variable declarations in your root module:
Using a Registry module:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = var.vpc_name # This variable must be declared
cidr = var.vpc_cidr # This variable must be declared
}Add the required variables in your root module:
variable "vpc_name" {
description = "Name of the VPC"
type = string
}
variable "vpc_cidr" {
description = "CIDR block for the VPC"
type = string
}Check the module's README or registry page for a complete list of supported inputs.
After adding the variable declarations, run terraform validate to check for syntax errors:
terraform validateThis command checks if your configuration is valid without requiring cloud credentials or state files. If validation passes, you're ready to run terraform plan or terraform apply.
# Plan the changes (shows what will happen)
terraform plan
# Apply the changes
terraform applyIf you get the error during plan/apply, it means the variable declarations are in place but values weren't provided. See the next step.
Variables without default values must be provided at runtime. You can supply values in several ways:
Via command-line:
terraform apply -var="aws_region=us-west-2" -var="environment=production"Via .tfvars file:
# terraform.tfvars
aws_region = "us-west-2"
environment = "production"
vpc_name = "my-vpc"Then run:
terraform applyVia environment variables (for string variables):
export TF_VAR_aws_region="us-west-2"
terraform applyAdd defaults to avoid requiring values:
variable "aws_region" {
type = string
default = "us-east-1"
}Variable Scope in Terraform:
- Root module variables are accessible in your root configuration and can be passed to modules
- Module variables are local to that module only - they cannot be accessed from other modules
- Each module has its own separate variable namespace
- Variables must be declared in the same directory level where they're used
Variable Type System:
Terraform supports several variable types: string, number, bool, list, map, set, object, and tuple. Using proper types helps catch configuration errors early:
variable "instance_count" {
type = number
}
variable "tags" {
type = map(string)
}
variable "availability_zones" {
type = list(string)
}Validation and Sensitive Variables:
Add validation rules to variables to ensure only valid values are accepted:
variable "environment" {
type = string
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Environment must be dev, staging, or prod."
}
}
variable "database_password" {
type = string
sensitive = true # Hides value from logs and output
}File Structure Best Practices:
- variables.tf - variable declarations
- main.tf - resource definitions
- outputs.tf - output values
- terraform.tfvars - variable values (add to .gitignore for sensitive values)
This organization makes variables easy to find and maintains consistency across projects.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform