Reference to undeclared module : No module call named module1 is declared in module.module2

Hi Guys, I have a setup in which there are two modules and two environment. module1’s output is being called at module2 using module.module1.module1-id while trying this I am getting error.

Reference to undeclared module : No module call named module1 is declared in module.module2

Directory structure:

Root

Modules | Environment

Modules:- module1, module2
Environment:- env1, env2

Are you calling module1 from module2 or calling both modules from same Terraform file?

Inside environment there is main.ft which comprises of some commonly used resources, data and locals. There are separate tf files for each components which are calling each respective modules.

Environment dir:-

main.ft, component1.tf, component2.tf

Component1.ft contains

module “module1” {
source = “…/…/modules/module1”

}

In module2 main.ft I am using one output of module1 (which is module.module1.module1-id) and i am getting error.

You can’t refer the output of module1 from module2 in that way unless you are calling the module1 from module2.
What you need to do in component1.tf is,

module "module2" {
  ...
  module1_parameter = module.module1.module1_id
  ..
}

Note that you’ll also need to update the module2 to have the additional parameter.

How can I refer module1 in module2 without actually triggering it ?. After making the changes I am getting this error.

Error:

Error: Reference to undeclared module

│ on component2.tf line 11, in module “module2”:
│ 11: module1_id = module.module1.module1-id

│ No module call named “module1” is declared in the root module. Did you mean “module1”?

=====

It’s hard to understand the issue without having a look at the code. Can you put the basic structure of the code in each .tf file here?

It is working while calling modules. Thanks