I am trying to use count to create multiple (var.VM_count = 2) VMs and their NICs. I am struggling to get the plan to run and assign the NICs to the corresponding VM resources. I keep receiving
Error: Error in function call
on …\Modules\AzureVM\Main.tf line 44, in resource “azurerm_virtual_machine” “vm”:
44: network_interface_ids = [“${element(azurerm_network_interface.NIC.*.id, count.index)}”]
|----------------
| azurerm_network_interface.NIC is empty tuple
| count.index is 1Call to function “element” failed: cannot use element function with an empty
list.
Here is section of the plan.
resource "azurerm_network_interface" "NIC" { count = "${var.VM_count}" name = "${var.VM_Name}${count.index}-nic" location = "${azurerm_resource_group.RG.location}" resource_group_name = "${var.RG}" ip_configuration { name = "${var.VM_Name}${count.index}-IP" subnet_id = "${var.vnet}" private_ip_address_allocation = "${var.IP_type}" } tags = { Billing = "${var.billing_tag}" Environment = "${var.OctoEnvName}" } } resource "azurerm_virtual_machine" "vm" { count = "${var.VM_count}" name = "${var.VM_Name}${count.index}" location = "${azurerm_resource_group.RG.location}" resource_group_name = "${var.RG}" network_interface_ids = ["${element(azurerm_network_interface.NIC.*.id, count.index)}"] vm_size = "${var.vm_size}" depends_on = ["azurerm_network_interface.NIC"] tags = { Billing = "${var.billing_tag}" Environment = "${var.OctoEnvName}" }
I am at a loss as to why the NIC isn’t creating and is only showing a count of 1 in the error. Any suggestions would great.
Thanks!