Azure,terraform 报告 network_interface_id 必须是单个值,而不是创建内部负载均衡器时的列表

Azure , terraform reports network_interface_id must be a single value, not a list when creating an internal Load balancer

我收到以下错误:

Error: azurerm_network_interface_backend_address_pool_association.tf-nilb-erx-sqlcl1[0]: network_interface_id must be a single value, not a list

Error: azurerm_network_interface_backend_address_pool_association.tf-nilb-erx-sqlcl1[1]: network_interface_id must be a single value, not a list

Terraform 代码如下:

resource "azurerm_network_interface_backend_address_pool_association" "tf-nilb-erx-sqlcl1" {
  count                   = "${var.count_sqlcl1_vm}"
  network_interface_id    = ["${element(azurerm_network_interface.tf-ni-erx-sqlcl1.*.id, count.index)}"]
  ip_configuration_name   = "erx-sha-pre-sqlcl1-fip-au-se"
  backend_address_pool_id = "${azurerm_lb_backend_address_pool.tf-bep-erx-sqlcl1.id}"
}
resource "azurerm_network_interface_backend_address_pool_association" "tf-nilb-erx-sqlcl2" {
  count                   = "${var.count_sqlcl2_vm}"
  network_interface_id    = ["${element(azurerm_network_interface.tf-ni-erx-sqlcl2.*.id, count.index)}"]
  ip_configuration_name   = "erx-sha-pre-sqlcl2-fip-au-se"
  backend_address_pool_id = "${azurerm_lb_backend_address_pool.tf-bep-erx-sqlcl2.id}"
}

基本上,我想在两个不同的可用性集中创建一个 SQL 虚拟机集群。一旦配置完成,他们每个人都需要一个内部负载平衡器。

网络接口代码如下:

resource "azurerm_network_interface" "tf-ni-erx-sqlcl1" {
 count               = "${var.count_sqlcl1_vm}"
 name                = "${var.sql_base_hostname}${format("%02d",count.index+1)}-nic01"
 location            = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
 resource_group_name = "${data.azurerm_resource_group.tf-rg-erx-external.name}"

ip_configuration {
    name                          = "${var.sql_base_hostname}${format("%02d",count.index+1)}-iip01"
    subnet_id                     = "${data.azurerm_subnet.tf-sn-erx-sql.id}"
    private_ip_address_allocation = "${var.env=="msdn"?"dynamic":"static"}"
    private_ip_address            ="10.112.3.${count.index+10}"
}
}

resource "azurerm_network_interface" "tf-ni-erx-sqlcl2" {
 count               = "${var.count_sqlcl2_vm}"
 name                = "${var.sql_base_hostname}${format("%02d",count.index+1)}-nic01"
 location            = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
 resource_group_name = "${data.azurerm_resource_group.tf-rg-erx-external.name}"

ip_configuration {
    name                          = "${var.sql_base_hostname}${format("%02d",count.index+1)}-iip01"
    subnet_id                     = "${data.azurerm_subnet.tf-sn-erx-sql.id}"
    private_ip_address_allocation = "${var.env=="msdn"?"dynamic":"static"}"
    private_ip_address            ="10.112.3.${count.index+15}"
}
}

是的,删除 [ 和 ] 使它工作,但让我想知道的是为什么下面的代码 有效 当我们使用 [] ?

resource "azurerm_virtual_machine" "tf-vm-erx-sqlcl1" {
  count                 = "${var.count_sqlcl1_vm}"
  name                  = "${var.sql_base_hostname}${format("%02d",count.index+1)}"
  location              = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
  resource_group_name   = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
  availability_set_id   = "${azurerm_availability_set.tf-as-erx-sqlcl1.id}"
  network_interface_ids = ["${element(azurerm_network_interface.tf-ni-erx-sqlcl1.*.id, count.index)}"]
  vm_size               = "${var.sqldbs_vm_size}"

我以为只要有计数就用方括号。

如有错误请指正

谢谢

如错误所示:

network_interface_id must be a single value, not a list

["${element(azurerm_network_interface.tf-ni-erx-sqlcl1.*.id, count.index)}"]表示列表。所以你只需要将它设置为 network_interface_id = "${element(azurerm_network_interface.tf-ni-erx-sqlcl1.*.id, count.index)}".