Undeclared input variable in root module

I’m relatively new to Terraform, so this confuses me.
I have a rather simple module to create an aws instance, cent7_base. I have a a tf file in the root to specify the specific things to the instance, server.tf

If I do a targetted plan it will says everything is fine, ready to go. However in our build environment we tend to deply with just tf plan to make sure we didn’t disrupt the rest of the infrastructure. With a tf plan I get

Error: Reference to undeclared input variable

on server.tf line 13, in module “server”:
13: var.vpc_security_group_ids,

An input variable with the name “vpc_security_group_ids” has not been
declared. This variable can be declared with a variable
“vpc_security_group_ids” {} block.

This variable exists in modules\cent7_base\variables.tf

So I’m uncertain what I’m missing. If I specify a variable in a module do I have to do something else to make sure it won’t raise issues when doing a plan on the entire infrastructure?

Hoping this is just something I’m forgetting to do.

It is difficult to be sure without seeing your code but you just want to set the value of vpc_security_group_ids = #... (note the lack of var.) within the module block. From the perspective of the code calling there is no var.vpc_security_group_ids unless you explicitly defined that with a variable block.

E.g., you want:

module "foobar" {
  source = "./modules/cent7_base"

  # Assuming it is an array of something
  vpc_security_group_ids = ["your", "values"]

}

Within the module code that is referenced as var.vpc_security_group_ids because the module defined it as a variable.

I hope that makes sense.

Thanks, sorry it’s taken me a bit. I’ve done some redacting to the module, but just with the specific names. Here’s what I have:

module "server" {
  source                    = "./modules/cent7_base"
  instance_count          = 1
  vpc_id                  = aws_vpc.<vpc>.id
  route_table_id          = aws_route_table.<route table>.id
  zone_id                 = aws_route53_zone.<zone>.zone_id
  availability_zones      = data.aws_availability_zones.available.names
  instance_type           = "t2.medium"
  key_name                = "<ssh key>"
  provides                = "server"

  vpc_security_group_ids = concat(
      var.vpc_security_group_ids,
      [aws_security_group.<sg_name>.id],
      [aws_security_group.<admin_sg>.id]
  )
}

My modules structure is basically:
modules
cent7_base
main.tf
variables.tf

In the modules variables.tf I have

variable "vpc_security_group_ids" {}

and
variable “provides” {}

Hope that paints a clearer picture. If I do a targetted
tf plan --target “module.server” -out “server.out”

This works fine.
But if I just do a terraform plan in the root to make sure it’s not changing anything else in the infrastructure I get:

Error: Reference to undeclared input variable

on server.tf line 13, in module “server”:
13: var.vpc_security_group_ids,

An input variable with the name “vpc_security_group_ids” has not been
declared. This variable can be declared with a variable
“vpc_security_group_ids” {} block.

Error: Reference to undeclared input variable

on server.tf line 25, in data “template_file” “server”:
25: hostname = var.provides

An input variable with the name “provides” has not been declared. This
variable can be declared with a variable “provides” {} block.

Actually I figured out my problem, was rather dumb. I was trying to define something in the module that didn’t exist yet. Oh well, this is why I’m still learning Terraform :slight_smile: Appreciate the help.

1 Like

I have a similar Problem. I am trying to deploy the following code listed below (sectioned by folder) and I receive the following error codes:

Error messages:
Error: Reference to undeclared input variable

  on main.tf line 12, in resource "azurerm_virtual_network" "web_server_vnet":
  12:     name                = "${var.resource_prefix}-vnet"

An input variable with the name "resource_prefix" has not been declared. This
variable can be declared with a variable "resource_prefix" {} block.


Error: Reference to undeclared input variable

  on main.tf line 15, in resource "azurerm_virtual_network" "web_server_vnet":
  15:     address_space       = [var.web_server_address_space]

An input variable with the name "web_server_address_space" has not been
declared. This variable can be declared with a variable
"web_server_address_space" {} block.

Main.tf:
provider “azurerm”{
version = “2.2.0”
features {}
}

resource "azurerm_resource_group" "web_server_rg" {
    name = var.web_server_rg
    location = var.web_server_location
}

resource "azurerm_virtual_network" "web_server_vnet"{
    name                = "${var.resource_prefix}-vnet"
    location            = var.web_server_location
    resource_group_name = azurerm_resource_group.web_server_rg.name
    address_space       = [var.web_server_address_space]
}

variables.tf:
variable “web_server_location” {
type = string
}

variable "web_server_rg" {
    type = string
}

variable "resource_prefix" {
    type = string 
}

variable "web_server_address_space" {
    type = string 
}

terraform.tfvars:
web_server_location = “westus2”
web_server_rg = “web-rg”
resource_prefix = “web-server”
web_server_address_space = “1.0.0.0/22”

What does that mean?
I’d understand that your variables.tf might be in a different folder than main.tf, so terraform doesn’t “combine” those files.

Sorry for the confusion. I meant “separate files” not folders. I am unsure how to address the errors since I already added the values the the variables that I declared in my code. Are you able to provide some guidance?

It reads like the file variables.tf cannot be found. Is it in the same folder with proper ending .tf?
Maybe format it using terraform fmt variables.tf or recreate it?

I just figured this out smh. And the code was correct but the files themselves were not saved. lesson learned, but thanks for your help!

1 Like