【问题标题】:Accessing the output from module via index通过索引访问模块的输出
【发布时间】:2018-01-01 00:27:20
【问题描述】:

我正在尝试使用 Terraform 在 Azure 上创建 2 个 VM。

我创建了 2 个 NIC,例如

variable "internalips" {
  description = "List of Internal IPs"
  default = ["10.0.2.10", "10.0.2.11"]
  type = "list"
}


resource "azurerm_network_interface" "helloterraformnic" {
count = 2
name = "nic-${count.index}"
location = "West US"
resource_group_name = "myrg"

    ip_configuration {
        name = "testconfiguration1"
        subnet_id = "${azurerm_subnet.helloterraformsubnet.id}"
        private_ip_address_allocation = "static"
        private_ip_address = "${element(private_ip_address, count.index)}"
    }
}

现在我想在模块 azurerm_virtual_machine 中使用它们

resource "azurerm_virtual_machine" "helloterraformvm" {
    count = 2
    name = "${element(elasticmachines, count.index)}"
    location = "West US"
    resource_group_name = "myrg"
    network_interface_ids = "${element(azurerm_network_interface.helloterraformnic, count.index)}"
....
}

这给了我一个错误

无法加载根配置模块:加载 azure/rg.tf 时出错:错误 读取 azurerm_virtual_machine[helloterraformvm] 的配置: azurerm_network_interface.helloterraformnic:资源变量必须 分三部分:TYPE.NAME.ATTR in:

${element(azurerm_network_interface.helloterraformnic, count.index)}

如何使用上面创建的 NIC 使用索引?

【问题讨论】:

    标签: terraform


    【解决方案1】:

    首先考虑使用length 函数来获取计数而不是硬编码。

    来自

    count = 2
    

    改成

    count = "${length(var.internalips)}"
    

    对于你的问题,你需要告诉资源你想获取哪个属性的值。

    network_interface_ids = "${element(azurerm_network_interface.helloterraformnic.id, count.index)}"
    

    参考:

    terraform Interpolation Syntax

    terraform azurerm_virtual_machine Attributes Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2015-10-13
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多