How to force dependencies

Hi all,

i would like to have the dependenciens resolved by terraform in the following code:

module "nsg" {
  for_each       = { for nsg in var.nsg_configs : => nsg if != "" }
  source         = "./modules/nsg"
  name           =
  security_rules = each.value.security_rules

module "ovm_public" {
  for_each            = { for ovm in var.ovm_public_configs : => ovm if != "" }
  source              = "./modules/ovm"
  nsg_id              = [for n in each.value.nsg : module.nsg[n].nsg_id[n]]
ovm_public_configs  = [{ name = "myovm", nsg = ["test1"], .... }]
nsg_configs = [{ name = "test1", security_rules = [{ direction = "ingress", ... }]}]

Maybe a solution can be move the module “nsg”, inside the module “ovm_public”, but i don’t think is the best solution. Can you suggest an idea?


Hi @hlocap,

With the configuration you shared, Terraform will automatically infer that everything which refers to var.nsg_id inside your ovm_public module must depend on everything mentioned in the output values of your nsg module, because the nsg_id definition refers to module.nsg.

Is that sufficient for what you need? If you need some additional dependency relationships then it would help if you can describe in more detail specifically which dependency relationships you need to declare.


i’ve changed the output vars nsg_id for module “nsg”, forcing the data source (nsg_id depends on it) to be read after the resource has been created. With this fix, the code works as expected.