Terraform - 多次计数的无效索引错误

标签 terraform terraform-provider-azure

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

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”

最佳答案

实际上,我认为您在问题中提供的 Terraform 代码没有任何问题,并且在我这边一切正常。

错误还说:

The given key does not identity an element this collection value.

这可能是因为您的公共(public) IP 没有在网络接口(interface)之前创建。真奇怪。 Terraform 将以正确的顺序对所有资源进行排序。也许你可以尝试升级 Terraform 版本。我用的是最新的版本:

Terraform v0.12.19
+ provider.azurerm v1.41.0

或者您可以尝试像这样更改代码:

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

关于Terraform - 多次计数的无效索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59771167/

相关文章:

azure - 在 Terraform for Azure 中使用 for_each 时如何获取 id 作为输出?

Azure 存储帐户防火墙规则阻止使用 azure devops 进行 terraform 部署

azure-functions - 如何获取使用 Terraform 部署的 Function-App 中的 "Function Url"?

azure - 使用 terraform 获取 Azure 函数应用程序 SLOT 默认主机 key

terraform - 更新到 terraform 版本 0.12.26 后出现 "Ambiguous attribute key"错误

amazon-web-services - 仅在代码更改时为 Lambda 创建新的 archive_file?

amazon-web-services - Terraform:创建 SG 时属性 "ingress"的值不合适

azure - WinRM 与远程执行连接超时 - Azure Terraform

terraform - 提供者变量是否可以在 terraform 中使用?

azure - 错误消息: The current SKU does not support 'private endpoint connection'