Is it possible to create a dependency on an external module?

Hello.
My current setup has several decoupled modules.
Sometimes, I need to create a resource in module foo which depends on module bar.
However, since foo and bar are decoupled, I cannot create the dependency because they are unaware of each other’s existence.

I know that one can instantiate a module inside another one as a child module. But the problem is that this would also instantiate everything inside the child module.

To give an example of my setup:

-root
--foo
---main.tf
--bar
---main.tf
--deploy 
---main.tf (instantiates both foo and bar as child modules)

Inside of bar.main.tf there is a module definition that should depend on another one in foo.main.tf.

I cannot instantiate foo.main.tf inside bar.main.tf because it will then instantiate everything inside foo.main.tf, which should only happen inside deploy.main.tf.

But that also means that I cannot establish a dependency. Is there a way to work around this?

Hi @dasph21 ,

Not sure if this will help you. You could output a value of bar.main.tf that you are interested to see in foo.main.tf. And within deploy.main.tf you could pass that as module input to foo.main.tf. That’ll create a dependency

so, within bar.output.tf you could do this,

output "bar_uuid" {
 value = random_uuid.bar_uuid
}

And within deploy.main.tf

module "bar" {
  source = "../modules/bar"
}
module "foo" {
  source = "../modules/foo"
  bar_uuid = module.bar.bar_uuid
}

That should create a dependency.
Apologies if I completely misunderstood your requirement.