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