amit suneja
amit suneja

Reputation: 43

terraform Vnet and Subnet Modules

I have a pipeline that downloads standard Terraform modules and creates resources.

  1. "Module-ResourceGroup" deploys resource group.
  2. "Module-Vnet" which deploys vnet.
  3. "Module-Subnet" which deploys subnets.

My problem is when I kick the pipeline for the first time my pipeline fails reason because Module-Subnet gives me an error message that Vnet does not exist. However, when I run the same pipeline for a second time, my Subnets get deployed without any issues as during the first run, then Vnet gets created.

I guess I have two solutions :

  1. depends_on where I can say that by subnet module is dependent on vnet module.
  2. Introduce a wait of 3 mins in the subnet module before it gets executed.

Q1. why it is happening? whereas as per terraform "Most of the time, Terraform infers dependencies between resources based on the configuration given" https://learn.hashicorp.com/tutorials/terraform/dependencies if anything is wrong with the way I have written modules?

Q2. What is a better solution depends_on OR introduce wait

Q3. Is there any other way to fix it?

Below are my modules.

Module-ResourceGroup/main.tf

resource "azurerm_resource_group" "my-resourcegroup" {
  name     = format("%s-%s",var.resource_group_name,var.env)
  location = var.location
}

Module-Vnet/main.tf

resource "azurerm_virtual_network" "my-vnet" {
  name                = format("%s-%s",var.vnet_name,var.env)
  resource_group_name = format("%s-%s",var.resource_group_name,var.env)
  location            = var.location
  address_space       = var.address_space
}

Module-Subnet/main.tf

resource "azurerm_subnet" "my-subnet" {
  for_each = var.subnetsconfig
    name                 = format("%s-%s",each.key,var.env)
    address_prefixes     = each.value["address_prefixes"]
    virtual_network_name = format("%s-%s",var.vnet_name,var.env)
    resource_group_name  = format("%s-%s",var.resource_group_name,var.env)
}

Upvotes: 0

Views: 1092

Answers (1)

Sebastian
Sebastian

Reputation: 440

If you use the output of a resource as the input of another resource then Terraform will understand it as an implicit dependency. For example (as you did not post all of your code):

Module-ResourceGroup/main.tf

resource "azurerm_resource_group" "my-resourcegroup" {
  name     = format("%s-%s",var.resource_group_name,var.env)
  location = var.location
}

Module-Vnet/main.tf

resource "azurerm_virtual_network" "my-vnet" {
  name                = format("%s-%s",var.vnet_name,var.env)
  resource_group_name = azurerm_resource_group.my-resourcegroup.name
  location            = var.location
  address_space       = var.address_space
}

Module-Subnet/main.tf

resource "azurerm_subnet" "my-subnet" {
  for_each             = var.subnetsconfig
  name                 = format("%s-%s",each.key,var.env)
  address_prefixes     = each.value["address_prefixes"]
  virtual_network_name = azurerm_virtual_network.my-vnet.name
  resource_group_name  = azurerm_resource_group.my-resourcegroup.name
}

Upvotes: 1

Related Questions