Terraform reports 'No module called "name of the module" is declared' in "root" module
S

2

6

I have done exploration and I want to stick customized module concept rather than to use the Azure public registry of modules.

Source code location is here

https://github.com/ameyaagashe/help_me_cross_2

I run terraform on command line with below arguments:

terraform plan -var "resource_group_name=nxt-grp-prd-manage-rgp-au-se" -var "virtual_network_name=virtual_network_1" -var "sql_subnet_name=subnet_1" -var "app_subnet_name=subnet_2" -var "application_nsg=test_nsg" -var "count_vm=2" -var "sql_host_basename=sqlvms" -var "app_host_basename=appvms" -var "storage_account_suffix=sta" -var "virtual_machine_size=Standard_B1ms" -var "virtual_machine_image_publisher=MicrosoftWindowsServer" -var "virtual_machine_image_offer=WindowsServer" -var "virtual_machine_image_sku=2012-R2-Datacenter" -var "virtual_machine_image_version=latest" -var "username=devopsadmin" -var "password=Angular12#$%"

However, I am getting errors like below:

Error: Reference to undeclared module

  on ../../modules/compute/main.tf line 25, in resource "azurerm_virtual_machine" "tf-vm":
  25:   location                         = module.resourcegroup.external_rg_location

No module call named "resourcegroup" is declared in sql_vms.


Error: Reference to undeclared module

  on ../../modules/compute/main.tf line 26, in resource "azurerm_virtual_machine" "tf-vm":
  26:   resource_group_name              = module.resourcegroup.external_rg_name

No module call named "resourcegroup" is declared in sql_vms.


Error: Reference to undeclared module

  on ../../modules/compute/main.tf line 27, in resource "azurerm_virtual_machine" "tf-vm":
  27:   network_interface_ids            = [element(module.network.network_interface_ids,count.index)]

No module call named "network" is declared in sql_vms.


Error: Reference to undeclared module

  on ../../modules/network/data.tf line 5, in data "azurerm_virtual_network" "tf-vn":
   5:   resource_group_name  = module.resource_groups.external_rg_name

No module call named "resource_groups" is declared in networking.


Error: Reference to undeclared module

  on ../../modules/nsg/main.tf line 3, in resource "azurerm_network_security_group" "tf-nsg":
   3:   location            = module.resourcegroup.external_rg_location

No module call named "resourcegroup" is declared in network_security_group.


Error: Reference to undeclared module

  on ../../modules/nsg/main.tf line 4, in resource "azurerm_network_security_group" "tf-nsg":
   4:   resource_group_name = module.resourcegroup.external_rg_name

No module call named "resourcegroup" is declared in network_security_group.


Error: Reference to undeclared input variable

  on ../../modules/resourcegroup/data.tf line 2, in data "azurerm_resource_group" "tf-rg-external":
   2:   name = var.rg_name

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

Unable to understand the underlying issue...

I am defining all modules in their own terraform configuration files and calling them in root module?

I would sincerely appreciate your assistance.

Sampler answered 2/12, 2019 at 3:25 Comment(1)
Remove the password from commandAvila
B
14

Working through each problem individually probably won't be helpful to you in the long-term, so I'm going to offer a general sense of what's wrong and share references to help with future Terraform configurations.

Referencing module.resource_group outputs from inside module.sql_vms won't work, because Terraform module composition doesn't allow sibling modules to directly reference outputs from each other.

There is a section in the documentation that explains it:

Outputs can only be passed from one module into another by way of the root module.

Here's an example from the Terraform documentation:

module "network" {
  source = "./modules/aws-network"

  base_cidr_block = "10.0.0.0/8"
}

module "consul_cluster" {
  source = "./modules/aws-consul-cluster"

  vpc_id     = module.network.vpc_id
  subnet_ids = module.network.subnet_ids
}

Once this is fixed, the next problem is the modules aren't being referenced by their correct name. According to the main.tf in the git repo, 4 modules have been declared:

module.sql_vms
module.resource_group
module.networking
module.network_security_group

Since the module is declared as resource_group, it should be referenced as such instead of resourcegroup.

Finally, a variable named rg_name should be declared inside the modules/resourcegroup/ directory. Please see the following to learn about variable declarations:

This may not be all the problems, but it's a start. If possible, I strongly recommend reading through the Terraform documentation on creating modules. They can be found here:

Besides answered 2/12, 2019 at 10:3 Comment(2)
Thanks a million mate. Understood it now.Sampler
But then it comes to module deployment order.Zygosis
C
0

Just a heads up to check the names/spellings for the modules and elements used in the files. This error must not occur if you think everything has been done right , there might be a possibility of typo error in terraform files.

Carcinomatosis answered 4/2, 2022 at 7:15 Comment(1)
I encountered a similar issue which was due to typo errorCarcinomatosis

© 2022 - 2024 — McMap. All rights reserved.