Conditional input to azure_virtual_machine - pass multiple network_interface_ids or create a nic if not present

Hi,

I’m building a small VM module for a custom requirement that will accept multiple azurerm_network_interface IDs passed into it from outside the module, or, if no network IDs are passed, the module will create a single network interface.

below code is in the module, simplifying/genericising for the forum:

modules\vm\vm.tf

resource "azurerm_windows_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = var.resource_group_name
  location            = var.location
  size                = var.vm_size
  admin_username      = "adminuser"
  admin_password      = "P@$$w0rd1234!"
  network_interface_ids = var.network_interface_ids == [] ? [azurerm_network_interface.interface.id] : var.network_interface_ids

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
}

modules\vm\nic.tf (ignore that this has no conditional to create yet):

resource "azurerm_network_interface" "interface" {
  name                = "IP-${var.vm_name}-01"
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
    name                          = "Primary"
    subnet_id                     = var.subnet_id
    private_ip_address_allocation = "Static"
    private_ip_address            = var.private_ip_address
  }

win-server.tf

module "win_vm" {
  source = "./modules/vm"

  for_each = var.win_virtual_machines

  vm_name = "somevm"
  resource_group_name = azurerm_resource_group.rg.name
  location = var.location
  vm_size = each.value["size"]
  subnet_id = azurerm_subnet.subnet.id
  private_ip_address = each.value["ip_address"]
}

params.tf

win_virtual_machines = {
  "vm1" = {
    "size" = "standard_F2"
    "ip_address" = "10.8.17.4"
  }
  "vm2" = {
    "size" = "standard_F2"
    "ip_address" = "10.8.17.5"
  }
}

here, where no nics are being passed in to var.network_interface_ids i’d expect the condititional to fall back to using the resource, but the error returned is:

 Error: Not enough list items
│
│   with module.win_vm["vm1"].azurerm_windows_virtual_machine.example,
│   on modules\virtual-machine\vm.tf line 13, in resource "azurerm_windows_virtual_machine" "example":
│   13:   network_interface_ids = var.network_interface_ids == [] ? [azurerm_network_interface.interface.id] : var.network_interface_ids
│
│ Attribute network_interface_ids requires 1 item minimum, but config has only 0 declared.
╵
╷
│ Error: Not enough list items
│
│   with module.win_vm["vm2"].azurerm_windows_virtual_machine.example,
│   on modules\virtual-machine\vm.tf line 13, in resource "azurerm_windows_virtual_machine" "example":
│   13:   network_interface_ids = var.network_interface_ids == [] ? [azurerm_network_interface.interface.id] : var.network_interface_ids
│
│ Attribute network_interface_ids requires 1 item minimum, but config has only 0 declared.

Is there a validation issue on this or have I mis-written my logic?