Search code examples
terraformazure-virtual-machineterraform-provider-azureazure-load-balancer

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


I am getting the error below:

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 code is below:

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}"
}

Basically, I want to create a cluster of SQL VM's in two different availability sets. Once they are provisioned they need to have an internal load balancer each.

Network Interface code is as below:

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}"
}
}

Yes, removing [ and ] made it work, but what makes me wonder is why the below code works when we are using [] ?

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}"

I thought we use, square bracket whenever there is a count used.

Correct me if I'm wrong.

Thanks


Solution

  • As the error shows:

    network_interface_id must be a single value, not a list

    The ["${element(azurerm_network_interface.tf-ni-erx-sqlcl1.*.id, count.index)}"] means a list. So you just need to set it as network_interface_id = "${element(azurerm_network_interface.tf-ni-erx-sqlcl1.*.id, count.index)}".