Terraform 通过索引访问模块的输出

Terraform 通过索引访问模块的输出,terraform,Terraform,我正在尝试使用Terraform在Azure上创建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-${

我正在尝试使用Terraform在Azure上创建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_虚拟机模块中使用它们

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_虚拟机[helloterraformvm]的配置: azurerm_network_interface.helloterraformnic:资源变量必须 分为三部分:TYPE.NAME.ATTR in:

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


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

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

改为

count = "${length(var.internalips)}"
对于您的问题,您需要告诉资源您希望获得哪个属性的值

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


首先考虑使用
length
函数来获得比硬编码更多的计数

改为

count = "${length(var.internalips)}"
对于您的问题,您需要告诉资源您希望获得哪个属性的值

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