Wait\Depend on resources in parent from module

terraform 0.12 depends on module
terraform module execution order
terraform inheritance
terraform reference existing resource
terraform output depends_on
terraform count
terraform reference resource in another file
terraform module for_each

I create the resource group and the vNet in the main.tf and I reference module in the same file. The problem is, module cannot access these resources from the module. related code (most of the code removed, only relevant parts left):

main.tf:

module "worker" {
  source = "./vmLoop"

  vmName = "worker"
  prefix = "${var.reference["name"]}"
  loop   = "${var.reference["workerCount"]}"
}

resource "azurerm_resource_group" "rg" {
  name     = "${var.reference["name"]}"
  location = "${var.reference["location"]}"
}

How do I reference this resource group in the module? inside main.tf I can do this: "${azurerm_resource_group.rg.name}". Not only that, if I want to use data inside the module, that would fail because the resource is not yet created (most of the code removed, only relevant parts left):

Module.tf:

data "azurerm_resource_group" "rg" {
  name = "${var.prefix}"
}

Everything works fine if I precreate resource group\vnet.

In arm template, I would add a dependsOn property. However, modules in terraform do not support depends_on.

There's a fairly hackish workaround to this.

You can use depends_on on output variables of upstream module in the downstream, BUT you must make sure you use the variable in the downstream module, not just use it in the depends_on clause.

Unfortunately, that's not an ideal solution and it might not be applicable in some situations.

Issue with depending on resource from different module · Issue , I have a module that calls another module named infrastructure. The parent module has a azurerm_virtual_machine_extension resource and the to work on resources in different modules concurrently rather than waiting for  I create the resource group and the vNet in the main.tf and I reference module in the same file. The problem is, module cannot access these resources from the module. related code (most of the code removed, only relevant parts left):

In the module you should be passing the subnetID or resource group, or whatever you are creating as a variable. For instance in your main config you might call the following:

module "vnet" {
  source              = "Azure/network/azurerm"
  resource_group_name = "${var.resource_group}"
  vnet_name           = "${terraform.workspace}-vnet"
  location            = "${var.arm_region}"
  address_space       = "${var.arm_network_address_space}"
  subnet_prefixes     = ["${var.arm_subnet1_address_space}", "${var.arm_subnet2_address_space}"]
  subnet_names        = ["subnet1", "subnet2"]

  tags = {
    environment = "${terraform.workspace}"
  }
}

Then invoke your vmdeploy module that using the subnetID as follows:

module "vmdeploy" {
 source = "./vmdeploy"
 subnetID = "${module.vnet.vnet_subnets[0]}"
}

By referencing the other module as a value for the vmdeploy module you are creating an implicit dependency that Terraform will recognize and include in the resource graph.

Within the module itself you would define a variable called subnetID like this:

var "subnetID" {}

Does that help?

Depends_on for module · Issue #1178 · hashicorp/terraform · GitHub, Either a way for a resource to depend on a module as a dependency or a way to Need to have modules that can be re-used atomically with parents azurerm_automation_module does not wait for import to complete. Now module.child2.null_resource.baz depends on the foo_id variable, which means it in turn depends on anything that variable depends on. This completes the support for output values and input variables in depends_on , which allows inter-module dependencies to be constructed indirectly.

You need to pass the name of the Resource Group to the module. i.e.:

module "worker" {
  source = "./vmLoop"

  rg     = "${azurerm_resource_group.rg.name}"    
  vmName = "worker"
  prefix = "${var.reference["name"]}"
  loop   = "${var.reference["workerCount"]}"
}

Then in the worker module code itself, you would use the rg var for the worker's Resource Group.

How to make Terraform Modules wait for resources to be created in , vpc_private_route module - main.tf variable "depends" { default = [] } resource "​null_resource" "depends_on" { triggers = { depends_on  Can you depend on a resource id within a module without first defining it as an output of that module? I have a terraform project with a bunch of elements defined at the top level for now. I've put security groups into a module, as they're pretty verbose (when using aws_security_group_rule ), and since I really only need the IDs elsewhere in

Set deployment order for resources, If you need the child resource to be deployed after the parent resource, you must explicitly state that dependency with the dependsOn property. The most popular answer is out of date with Terraform in version 0.12.24. depends_on is a protected variable, and cannot be used in a module.In addition there are a few syntax differences.

Creating Wait Conditions in a Template, AWS CloudFormation creates a wait condition just like any other resource. The wait condition's Count property specifies the number of success signals. If none  Modules don't depend on each other, resources inside them do. This is exactly correct.

TIPS: Howto implement Module depends_on emulation, Add the following line to the resource in this module that depends on the Terraform to wait until the dependant external resources are created  There are a few ways to share resources across multiple projects or modules: Cut and paste them. Use Assembly and Dependency plugins; Use the maven-remote-resources-plugin; In this blog post I'll show how to do the second option since in my opinion, it is currently the most stable and flexible.

Comments
  • hey, can you provide some example code, i dont really understand this explanation, sorry
  • A code snippet will be WAY TOO LONG, so I will explain here. The idea is that, when you are calling a module you are most probably going to have output variables. You can use those values in other modules. So, what you can do is, use the depends_on = output_var clause when calling the second moudle. But you must make sure, that somewhere inside that second module, you use the output variable you have passed to your depends_on clause. I am not profficient in Azure, but in AWS, e.g. Create VPC > Pass ID as depends_on value > Use VPC_ID in the second module.
  • in my case i dont have output value and I dont have a module thats preceding this module, only main.tf that calls the module. as you clearly know what you are talking about - you got a link to aws example?
  • Unfortunately, I don't , I used this approach on a project I worked for, but we can start a chat so we don't spam here and discuss better.
  • Bah, an option here in the comments should show, where you start it ...., thought it was already there
  • hey Ned, thanks (and thanks for your course on tf)! this is more or less what I'm doing right now as a wordaround. I was hoping for a less hackish way
  • yeah, but like I said, the problem is the subnet resource, rg resource is just to showcase.