无法使 Terraform 模块依赖于资源 [英] Unable to make Terraform module dependent on resource

查看:37
本文介绍了无法使 Terraform 模块依赖于资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临 terraform 模块和资源之间的依赖问题.我的模块依赖于一个资源,但我无法找到如何在 Terraform 中明确暗示这一点.在它所依赖的资源创建之前运行模块代码的那一刻,这自然会导致错误.

I am facing a dependency problem between terraform module and resource. My module is dependent on a resource but I have not been able to find out how to explicitly imply this in Terraform. At the moment the module code is ran before the resource it is dependent on is created, this is naturally resulting in an error.

我浏览了互联网,发现很少有关于模块依赖于其他模块的讨论,包括一些关于如何解决模块中缺少depends_on"的建议.但是,当模块依赖资源时,我无法找到任何建议/解决方法.

I have looked across the internet and found few discussions around modules having dependencies on other modules including some suggestions on how to work around the lack of "depends_on" in modules. However I have not been able to find any suggestion/ workarounds for when modules have a dependency on resource.

我尝试了以下建议但无济于事

I have tried suggestion made on following but to no avail

https://github.com/hashicorp/terraform/issues/16983

https://devops.stackexchange.com/questions/6163/how-to-make-terraform-modules-wait-for-resources-to-be-created-in-place-of-using

下面是我的标准 azure nic 和 vm 模块的代码

Below is the code for my module for standard azure nic and vm

     resource "azurerm_network_interface" "nic" {
      name                = "${var.nicName}"
      resource_group_name = "${var.rgName}"
      location            = "${var.rgLocation}"

      ip_configuration {
        name                          = "vm-nic-configuration"
        subnet_id                     = "${var.subnetId}"
        private_ip_address_allocation = "dynamic"
      }
    }


    resource "azurerm_virtual_machine" "vms" {
      name                  = "${var.vmHostName}"
      location              = "${var.rgLocation}"
      resource_group_name   = "${var.rgName}"
      network_interface_ids = ["${azurerm_network_interface.nic.id}"]

      vm_size                          = "${var.vmSize}"
      delete_os_disk_on_termination    = true
      delete_data_disks_on_termination = true

      storage_image_reference {
        publisher = "MicrosoftWindowsServer"
        offer     = "WindowsServer"
        sku       = "2016-Datacenter"
        version   = "latest"
      }

      storage_os_disk {
        name              = "${var.diskVol01Name}"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
      }

      os_profile {
        computer_name  = "${var.vmHostName}"
        admin_username = "${var.vmUserName}"
        admin_password = "${var.vmAdminPwd}"
      }

      os_profile_windows_config {
        provision_vm_agent        = true
        enable_automatic_upgrades = true
      }

      depends_on = ["azurerm_network_interface.nic"]  
    }

这就是我在主模板文件中使用模块的方式

This is how I am using the module in my main template file

module "WinWeb01" {
  source = "../modules/vmsetup"

  nicName    = "${var.prefix}-web-nic01"
  rgName     = "${azurerm_resource_group.rg.name}"
  rgLocation = "${azurerm_resource_group.rg.location}"
  vmHostName = "${var.prefix}-web01"
  vmUserName = "vmAdmin"

  vmAdminPwd           = "${data.azurerm_key_vault_secret.vmPassword.0.value}"
  availabilitySetId    = "${azurerm_availability_set.webvmavailset.id}"
  vmSize               = "${var.vmSize}"
  diskVol01Name        = "${var.prefix}-web01-disk01"
  availabilitySetCount = "${var.availabilitySetCount}"
  subnetId             = "${azurerm_subnet.subnets.1.id}"
}

我需要在不同的子网中启动 VM,所以我认为如果我也将网络接口模板添加到我的模块中并在调用模块时提供适当的子网 ID 会很有帮助.

I have a requirement to spin up VMs in different subnets so I thought it will be beneficial if I add Network Interface template to my module as well and provide the appropriate subnet ids when calling the module.

但是问题是当我运行 terraform plan 时出现以下错误

However the problem is when I run terraform plan I get following error

module.WinWeb01.var.subnetId: Resource 'azurerm_subnet.subnets' not found for variable 'azurerm_subnet.subnets.1.id'

该模块在实际创建子网之前查找子网 ID(子网代码在主模板文件中).我看到很少有帖子建议使用depends_on 变量,但这在我的情况下不起作用.

The module is looking for a subnet id before the subnet has actually been created (subnet code is in main template file). I have seen few posts suggesting using depends_on variable but that has not worked in my case.

https://medium.com/@bonya/terraform-adding-depends-on-to-your-custom-modules-453754a8043e

如果有人能在这里指导我的正确方向,我将不胜感激.

Will appreciate if you anyone can guide my in a right direction here.

推荐答案

通过将 Terraform 升级到最新版本 0.12.2 解决了该问题.我之前用的是 v0.11.2.

Resolved the issue by upgrading Terraform to latest v 0.12.2. I was previously using v0.11.2.

这篇关于无法使 Terraform 模块依赖于资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆