Skip to content
Latchkey

Terraform "Reference to undeclared input variable" in CI

Every var.X reference must have a corresponding variable "X" block in the same module. When it does not, validate and plan stop with a reference to an undeclared input variable, pointing at the offending line.

What this error means

validate or plan fails with "Error: Reference to undeclared input variable" and "There is no variable named ..." at the file and line where var.X is used.

Terraform
Error: Reference to undeclared input variable

  on main.tf line 12, in resource "aws_instance" "web":
  12:   instance_type = var.instance_typ

There is no variable named "instance_typ".

Common causes

A typo or missing variable block

The name is misspelled, or the variable was used before its variable block was added to the module.

The variable lives in a different module

Variables are module-scoped; referencing a child module variable from the root (or vice versa) fails because it is not declared there.

How to fix it

Declare the variable in this module

  1. Add a variable block matching the referenced name in the same module.
  2. Pass its value via tfvars, -var, or the module call.
  3. Run validate to confirm the reference resolves.
variables.tf
variable "instance_type" {
  type    = string
  default = "t3.micro"
}

Fix the reference name

Correct the typo so var.X matches an existing variable block exactly, since names are case sensitive.

How to prevent it

  • Run terraform validate in CI before plan to catch undeclared names.
  • Keep variable declarations in the same module that uses them.
  • Pass child module inputs explicitly through the module block.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →