Terraform - 多次计数的无效索引错误
Terraform - Invalid index error with multiple count
我正在尝试配置 2 个 Public 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
时,我收到一条错误消息:
Terraform 版本为 "v0.12.3"
,Azure 提供商版本为 "v1.40.0"
鉴于这是 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}"
}
实际上,我认为您在问题中提供的 Terraform 代码没有任何问题,并且在我这边一切正常。
错误还说:
The given key does not identity an element this collection value.
可能是因为您的 public IP 没有在网络接口之前创建。真奇怪。 Terraform 将以正确的顺序对所有资源进行排序。也许你可以尝试升级 Terraform 版本。我用的是最新版本:
Terraform v0.12.19
+ provider.azurerm v1.41.0
或者您可以尝试像这样更改代码:
public_ip_address_id = "${element(azurerm_public_ip.example.*.id, count.index)}"
你可以尝试像这样更改代码,它适用于我:
public_ip_address_id = "${element(azurerm_public_ip.example.*.id, count.index)}"
我正在尝试配置 2 个 Public 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
时,我收到一条错误消息:
Terraform 版本为 "v0.12.3"
,Azure 提供商版本为 "v1.40.0"
鉴于这是 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}"
}
实际上,我认为您在问题中提供的 Terraform 代码没有任何问题,并且在我这边一切正常。
错误还说:
The given key does not identity an element this collection value.
可能是因为您的 public IP 没有在网络接口之前创建。真奇怪。 Terraform 将以正确的顺序对所有资源进行排序。也许你可以尝试升级 Terraform 版本。我用的是最新版本:
Terraform v0.12.19
+ provider.azurerm v1.41.0
或者您可以尝试像这样更改代码:
public_ip_address_id = "${element(azurerm_public_ip.example.*.id, count.index)}"
你可以尝试像这样更改代码,它适用于我:
public_ip_address_id = "${element(azurerm_public_ip.example.*.id, count.index)}"