Terraform-具有多个计数的无效索引错误

时间:2020-01-16 13:54:41

标签: terraform terraform-provider-azure

我正在尝试配置2个公共IP地址和2个网络接口。到目前为止,我写的是:

resource "azurerm_public_ip" "example" {
  name                    = "test-pip${count.index}"
  count = 2
  location                = "${azurerm_resource_group.rc.location}"
  resource_group_name     = "${azurerm_resource_group.rc.name}"
  allocation_method       = "Dynamic"
  idle_timeout_in_minutes = 30
}
output "public_ip_address" {
  value = "${azurerm_public_ip.example.*.id}"
}

resource "azurerm_network_interface" "main" {
  name                = "test${count.index}"
  count = 2
  location            = "${azurerm_resource_group.rc.location}"
  resource_group_name = "${azurerm_resource_group.rc.name}"

    ip_configuration {
    name                          = "testconfiguration1${count.index}"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = "${azurerm_public_ip.example[count.index].id}"
  }
}

稍后,我将使用这两个IP和NI将它们分配给2台VM计算机。

当我运行terraform plan时,出现一条错误消息:

enter image description here

Terraform版本为"v0.12.3",Azure提供者版本为"v1.40.0"

2 个答案:

答案 0 :(得分:1)

鉴于这是Terraform 0.12,而不是问题语法所暗示的Terraform 0.11,所以实际错误出在特定的导出属性中。要访问azurerm_public_ip.example资源导出的ip地址,我们需要使用ip_address导出的属性,而不是id。这就是为什么为无效密钥抛出错误的原因,尽管错误中的特定引用确实确实具有误导性。

我们可以通过以下方式更新您的代码以解决此问题:

ip_configuration {
  name                          = "testconfiguration1${count.index}"
  subnet_id                     = "${azurerm_subnet.internal.id}"
  private_ip_address_allocation = "Dynamic"
  public_ip_address_id          = "${azurerm_public_ip.example[count.index].ip_address}"
}

答案 1 :(得分:0)

你可以尝试像这样更改代码,它对我有用:

public_ip_address_id = "${element(azurerm_public_ip.example.*.id, count.index)}"