How to fix Error: Reference to undeclared input variable in Terraform

TerraformBEGINNERHIGHUpdated Dec 30, 2025· 170 views

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.

Quick fix

Fixes Error: Reference to undeclared input variable

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.
  1. Identify which variable is missing
  2. Add the variable declaration
  3. Check for typos in variable names
  4. Ensure variables are in the correct module scope
terraform — error
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.
Error: Reference to undeclared input variable

What this error means

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.

Typical symptoms

  • Terraform shows error: 'Reference to undeclared input variable'
  • The error message specifies which variable is missing (e.g., 'aws_region')
  • terraform plan or terraform apply fails before attempting any resource changes
  • Error appears immediately after running terraform init or terraform plan
  • Using a module from the Terraform Registry with missing variable declarations

Common causes

  1. Variable is used in the code (var.something) but never declared with a variable block
  2. Typo in variable name between declaration and usage (e.g., declared as my_variable but used as my_variabl)
  3. Variable declared in wrong module or wrong file - each module has its own variable scope
  4. Variable name is case-sensitive and the case doesn't match between declaration and usage
  5. Using a module that requires input variables but not providing variable declarations in your root module

How to fix "Error: Reference to undeclared input variable"

1Identify which variable is missing

Read the error message carefully to see which variable is undeclared. The error will show the exact variable name that's missing:

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

2Add the variable declaration

Create a new variable block in your Terraform configuration. The standard practice is to create a variables.tf file in the same directory:

hcl
# 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:

hcl
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

3Check for typos in variable names

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:

hcl
variable "my_variable" {
  type = string
}

resource "aws_instance" "example" {
  # Typo: my_variabl is missing the 'e'
  tags = {
    Name = var.my_variabl
  }
}

Correct:

hcl
variable "my_variable" {
  type = string
}

resource "aws_instance" "example" {
  tags = {
    Name = var.my_variable
  }
}

Use grep or search to verify consistency:

bash
# Check all occurrences of the variable
grep -r "my_variable" .
4Ensure variables are in the correct module scope

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):

hcl
module "networking" {
  source = "./modules/networking"

  cidr_block = var.vpc_cidr  # References root module variable
}

variable "vpc_cidr" {
  type = string
}

Module (modules/networking/main.tf):

hcl
resource "aws_vpc" "main" {
  cidr_block = var.vpc_cidr  # ERROR - this variable is not in this module's scope
}

Module (modules/networking/variables.tf):

hcl
variable "vpc_cidr" {
  type = string
}

Module (modules/networking/main.tf) - Corrected:

hcl
resource "aws_vpc" "main" {
  cidr_block = var.vpc_cidr  # Now this works because vpc_cidr is declared in this module
}
5Add variable declarations when using Registry modules

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:

hcl
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:

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

6Validate your configuration

After adding the variable declarations, run terraform validate to check for syntax errors:

bash
terraform validate

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

bash
# Plan the changes (shows what will happen)
terraform plan

# Apply the changes
terraform apply

If you get the error during plan/apply, it means the variable declarations are in place but values weren't provided. See the next step.

7Provide values for variables without defaults

Variables without default values must be provided at runtime. You can supply values in several ways:

Via command-line:

bash
terraform apply -var="aws_region=us-west-2" -var="environment=production"

Via .tfvars file:

hcl
# terraform.tfvars
aws_region = "us-west-2"
environment = "production"
vpc_name = "my-vpc"

Then run:

bash
terraform apply

Via environment variables (for string variables):

bash
export TF_VAR_aws_region="us-west-2"
terraform apply

Add defaults to avoid requiring values:

hcl
variable "aws_region" {
  type = string
  default = "us-east-1"
}

Advanced notes

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:

hcl
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:

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

Frequently asked questions

What does "Error: Reference to undeclared input variable" mean?
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.
How do I fix "Error: Reference to undeclared input variable"?
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.
What causes "Error: Reference to undeclared input variable"?
Variable is used in the code (var.something) but never declared with a variable block Typo in variable name between declaration and usage (e.g., declared as my_variable but used as my_variabl) Variable declared in wrong module or wrong file - each module has its own variable scope
Is the "Error: Reference to undeclared input variable" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading