Depends_on not working

727 views
Skip to first unread message

Paul Sanders

unread,
Aug 16, 2018, 6:53:14 AM8/16/18
to Terraform
Hi All,

This is my first post on here, and am fairly new to Terraform, so apologies if this is down to user error. 

I have a basic main.tf file that calls upon two sub modules to deploy some Azure resources, network and storage. This is my main.tf:

# Configure the Azure Provider
provider "azurerm" {}

module "storage" {
 source = "modules/storage"

  #depends_on                = ["${azurerm_resource_group.RG01.name}"]
 storageResourceGroupName  = "${var.storageResourceGroupName}"
 location                  = "${var.location}"
 vhdStorageName            = "${var.vhdStorageName}"
 vhdStorageSku             = "${var.vhdStorageSku}"
 vhdStorageReplicationType = "${var.vhdStorageReplicationType}"
 ScriptReplicationType     = "${var.ScriptReplicationType}"
 scriptStorageName         = "${var.scriptStorageName}"
 scriptStorageSku          = "${var.scriptStorageSku}"
 diagnosticsStorageName    = "${var.diagnosticsStorageName}"
 diagnosticsStorageSku     = "${var.diagnosticsStorageSku}"
 DiagnosticReplicationType = "${var.DiagnosticReplicationType}"
}

module "network" {
 source                            = "modules/network"
 networkResourceGroupName          = "${var.networkResourceGroupName}"
 VNetName                          = "${var.VNetName}"
 NetworkCIDR                       = "${var.NetworkCIDR}"
 SubnetGatewayName                 = "${var.SubnetGatewayName}"
 SubnetGatewayCIDR                 = "${var.SubnetGatewayCIDR}"
 applicationGatewayGreenSubnetName = "${var.applicationGatewayGreenSubnetName}"
 applicationGatewaySubnetCidrGreen = "${var.applicationGatewaySubnetCidrGreen}"
 applicationGatewayBlueSubnetName  = "${var.applicationGatewayBlueSubnetName}"
 applicationGatewaySubnetCidrBlue  = "${var.applicationGatewaySubnetCidrBlue}"
 jumpboxSubnetName                 = "${var.jumpboxSubnetName}"
 jumpboxSubnetCidr                 = "${var.jumpboxSubnetCidr}"
 scaleSetBlueSubnetName            = "${var.scaleSetBlueSubnetName}"
 scaleSetSubnetCidrBlue            = "${var.scaleSetSubnetCidrBlue}"
 scaleSetGreenSubnetName           = "${var.scaleSetGreenSubnetName}"
 scaleSetSubnetCidrGreen           = "${var.scaleSetSubnetCidrGreen}"
}


And here is network.tf in modules/network:
resource "azurerm_resource_group" "NetworkResourceGroup01" {
 name     = "${var.networkResourceGroupName}"
 location = "${var.location}"

  tags {
   environment = ""
 }
}

resource "azurerm_virtual_network" "vnet" {
 name                = "${var.VNetName}"
 address_space       = "${var.NetworkCIDR}"
 location            = "${var.location}"
 resource_group_name = "${var.networkResourceGroupName}"
 depends_on          = ["azurerm_resource_group.NetworkResourceGroup01"]

  subnet {
   name           = "${var.SubnetGatewayName}"
   address_prefix = "${var.SubnetGatewayCIDR}"
 }

  subnet {
   name           = "${var.applicationGatewayGreenSubnetName}"
   address_prefix = "${var.applicationGatewaySubnetCidrGreen}"
 }

  subnet {
   name           = "${var.applicationGatewayBlueSubnetName}"
   address_prefix = "${var.applicationGatewaySubnetCidrBlue}"
 }

  subnet {
   name           = "${var.jumpboxSubnetName}"
   address_prefix = "${var.jumpboxSubnetCidr}"
 }

  subnet {
   name           = "${var.scaleSetGreenSubnetName}"
   address_prefix = "${var.scaleSetSubnetCidrGreen}"
 }

  subnet {
   name           = "${var.scaleSetBlueSubnetName}"
   address_prefix = "${var.scaleSetSubnetCidrBlue}"
 }
}


and finally, my storage.tf in modules/storage.tf

resource "azurerm_resource_group" "StorageResourceGroup01" {
 name     = "${var.storageResourceGroupName}"
 location = "${var.location}"

  tags {
   environment = ""
 }
}

resource "azurerm_storage_account" "VHDStorageAccount" {
 name                     = "${var.vhdStorageName}"
 resource_group_name      = "${var.storageResourceGroupName}"
 location                 = "${var.location}"
 account_tier             = "${var.vhdStorageSku}"
 account_replication_type = "${var.vhdStorageReplicationType}"
 depends_on               = ["azurerm_resource_group.StorageResourceGroup01"]

  #depends_on = ["${var.depends_on}"]
}

resource "azurerm_storage_account" "ScriptStorageAccount" {
 name                     = "${var.scriptStorageName}"
 resource_group_name      = "${var.storageResourceGroupName}"
 location                 = "${var.location}"
 account_tier             = "${var.scriptStorageSku}"
 account_replication_type = "${var.ScriptReplicationType}"

  depends_on = ["azurerm_resource_group.StorageResourceGroup01"]
}

resource "azurerm_storage_account" "DiagnosticStorageAccount" {
 name                     = "${var.diagnosticsStorageName}"
 resource_group_name      = "${var.storageResourceGroupName}"
 location                 = "${var.location}"
 account_tier             = "${var.diagnosticsStorageSku}"
 account_replication_type = "${var.DiagnosticReplicationType}"
 depends_on               = ["azurerm_resource_group.StorageResourceGroup01"]

  #depends_on = ["${var.depends_on}"]
}


When I run a terraform plan, I get:

* azurerm_virtual_network.vnet: Error Creating/Updating Virtual Network "PSandersTestNet01" (Resource Group "NetworkTest"): network.VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceGroupNotFound" Message="Resource group 'NetworkTest' could not be found."

If I re-run it again, all goes through as the resource group has been created. So it is as though depends_on is being ignored?

Thanks

Paul 

Steven Nemetz

unread,
Aug 17, 2018, 12:26:34 AM8/17/18
to Terraform
It is being ignored or silently failing
Depends on doesn't work with modules. It only works on resources that are all in the same scope. So, both what is depended on and what needs it would need to be in the same module.


Paul Sanders

unread,
Aug 17, 2018, 4:08:03 AM8/17/18
to terrafo...@googlegroups.com
Thanks for the reply Steven. 

Do we know when cross module dependancies will come into play? Would be really helpful (even if it was waiting for the module to apply before running the next).

Regarding the storage.rf one though, the depends_on is within the same scope and module, but is still intermittent when it comes to being followed. Is there a way I can dig deeper with looking at the apply process?

Thanks
---
Kind Regards

Paul Sanders
Mob: 07988 725 883
Mail: paul.sa...@googlemail.com


On Fri, 17 Aug 2018 at 05:26, Steven Nemetz <sne...@hotmail.com> wrote:
It is being ignored or silently failing
Depends on doesn't work with modules. It only works on resources that are all in the same scope. So, both what is depended on and what needs it would need to be in the same module.


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/536f18af-d922-44ed-a07e-38402643a3eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jorge Ernesto Guevara Cuenca

unread,
Aug 17, 2018, 11:41:26 AM8/17/18
to terrafo...@googlegroups.com

For more options, visit https://groups.google.com/d/optout.


--
Jorge Ernesto Guevara Cuenca | DevOps Engineer
GLOBANT
 | CO: +57 1 489 1340 ext. 44866 | US: +1 877 215 5230 ext. 44866 |
FacebookTwitterYoutubeLinkedinPinterestGlobant

 



The information contained in this e-mail may be confidential. It has been sent for the sole use of the intended recipient(s). If the reader of this message is not an intended recipient, you are hereby notified that any unauthorized review, use, disclosure, dissemination, distribution or copying of this communication, or any of its contents, is strictly prohibited. If you have received it by mistake please let us know by e-mail immediately and delete it from your system. Many thanks.

 

La información contenida en este mensaje puede ser confidencial. Ha sido enviada para el uso exclusivo del destinatario(s) previsto. Si el lector de este mensaje no fuera el destinatario previsto, por el presente queda Ud. notificado que cualquier lectura, uso, publicación, diseminación, distribución o copiado de esta comunicación o su contenido está estrictamente prohibido. En caso de que Ud. hubiera recibido este mensaje por error le agradeceremos notificarnos por e-mail inmediatamente y eliminarlo de su sistema. Muchas gracias.


Steven Nemetz

unread,
Aug 17, 2018, 11:52:44 PM8/17/18
to Terraform

While that example may work or at least sometimes. I believe doing that will be problematic.
Depends_on is given a list of resources that can be seen in the current scope. x.y in one module is not the same as x.y in another module. Each of these has a different absolute path in the Terraform state. Therefore they are different resources. depends_on may work if provided the resource's absolute path instead of the relative one (never tried that). But that would be very difficult to maintain.

As far as cross module dependencies. There isn't. It is all resource dependencies based on variable passing.
So, if you pass module X the value module.y.a, anything that using that value in module X now depends on module y. Since y has to finish to have outputs. Depending on how module X is written the whole module might depend on y. But remember that will be a calculated field. So, it can't be used in a count statement.
Reply all
Reply to author
Forward
0 new messages