简体   繁体   English

terraform .net 和 Su.net 模块

[英]terraform Vnet and Subnet Modules

I have a pipeline that downloads standard Terraform modules and creates resources.我有一个管道可以下载标准 Terraform 模块并创建资源。

  1. "Module-ResourceGroup" deploys resource group. “Module-ResourceGroup”部署资源组。
  2. "Module-.net" which deploys .net.部署 .net 的“Module-.net”。
  3. "Module-Su.net" which deploys su.nets.部署 su.net 的“Module-Su.net”。

My problem is when I kick the pipeline for the first time my pipeline fails reason because Module-Su.net gives me an error message that .net does not exist.我的问题是,当我第一次启动管道时,我的管道失败了,因为Module-Su.net 给我一条错误消息,指出 .net 不存在。 However, when I run the same pipeline for a second time, my Su.nets get deployed without any issues as during the first run, then .net gets created.但是,当我第二次运行相同的管道时,我的 Su.net 部署没有任何问题,就像第一次运行时一样,然后创建了 .net。

I guess I have two solutions:我想我有两个解决方案:

  1. depends_on where I can say that by su.net module is dependent on .net module. depends_on 我可以说 su.net 模块依赖于 .net 模块。
  2. Introduce a wait of 3 mins in the su.net module before it gets executed.在执行前在 su.net 模块中引入 3 分钟的等待时间。

Q1. 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?而根据 terraform “大多数时候,Terraform 根据给定的配置推断资源之间的依赖关系” https://learn.hashicorp.com/tutorials/terraform/dependencies如果我编写模块的方式有任何问题?

Q2. Q2。 What is a better solution depends_on OR introduce wait什么是更好的解决方案 depends_on OR introduce wait

Q3. Q3. Is there any other way to fix it?还有其他方法可以解决吗?

Below are my modules.以下是我的模块。

Module-ResourceGroup/main.tf模块资源组/main.tf

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

Module-.net/main.tf模块-.net/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-Su.net/main.tf模块-Su.net/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)
}

If you use the output of a resource as the input of another resource then Terraform will understand it as an implicit dependency.如果您使用资源的 output 作为另一个资源的输入,那么 Terraform 将把它理解为隐式依赖。 For example (as you did not post all of your code):例如(因为您没有发布所有代码):

Module-ResourceGroup/main.tf模块资源组/main.tf

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

Module-.net/main.tf模块-.net/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-Su.net/main.tf模块-Su.net/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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM