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.
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 Appreciate the help.
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 {}
}
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?