我正在尝试配置 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
时,我收到一条错误消息:
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/