Terraform reporting error “cannot use element function with an empty”

My NIC creation code is as below:

resource "azurerm_network_interface" "tf-ni-test" {
 count                     = "${var.count_sss_vm}"
 name                      = "${var.sss_base_hostname}${format("%02d",count.index+1)}-nic01"
 #location                 = "${data.azurerm_resource_group.tf-rg-test-external.location}"
 location                  = "${var.location}"  
 resource_group_name       = "${data.azurerm_resource_group.tf-rg-test-external.name}"

ip_configuration {
    name                          = "${var.sss_base_hostname}${format("%02d",count.index+1)}-iip01"
    subnet_id                     = "${data.azurerm_subnet.tf-sn-test.id}"
    private_ip_address_allocation = "${var.env=="msdn"?"dynamic":"static"}"
    #private_ip_address            = "10.112.2.${count.index+10}"
    public_ip_address_id          = "${element(azurerm_public_ip.tf-pip-test.*.id,count.index+1)}"

VM creation code is as below:

resource "azurerm_virtual_machine" "tf-vm-test" {
  count                 = "${var.count_sss_vm}"
  name                  = "${var.sss_base_hostname}${format("%02d",count.index+1)}"
  location              = "${var.location}"  
  #location              = "${data.azurerm_resource_group.tf-rg-test-external.location}"
  resource_group_name   = "${data.azurerm_resource_group.tf-rg-test-external.name}"
  network_interface_ids = ["${element(azurerm_network_interface.tf-ni-test.*.id, count.index)}"]
  vm_size               = "${var.sss_vm_size}"

I’m running terraform plan with below command:

terraform plan -var "application_nsg=test-sss" -var "count_sss_vm=2" -var "env=msdn" -var "username=devopsadmin" -var "password=Angular1@#$" -var "sss_base_hostname=testsss" -var "sss_vm_size=Standard_B2s" -var "storage_account_suffix=sta" -var "win_image_offer=Windows" -var "win_image_publisher=microsoftvisualstudio" -var "win_sku=Windows-10-N-x64" -var "location=australiaeast"

Curious to know why it is reporting an error and how to resolve it. The same code has well before. Is this because of TF v0.12?

Doing exactly the way described below:

Hi @ameyaagashe,

Try this expression instead:

  network_interface_ids = [azurerm_network_interface.tf-ni-test[count.index].id]

The element function is still in Terraform 0.12 only for the special “wrap around” behavior it implements if the given index is greater than the last element of the list. The index syntax with [ ] is the main way to access a specific element from a list.

If you are upgrading from Terraform 0.11 to Terraform 0.12 with an existing codebase, I’d strongly recommend following the upgrade guide first before making any specific updates manually. The automatic upgrade tool can fix a number of potential problems during the upgrade, but it only works with configuration written in the Terraform 0.11 language so you’ll need to run that tool first and then make any other updates you want to make or need to make that the tool wasn’t able to address automatically.

OK, I would try out your suggestion. We are already using v0.12.5 The funny thing I noticed it, it sometimes works and sometimes does not for same parameters.

The suggestion does not work in fact it reports error, I however tried to delete the state file and then it works. Is this some sort of bug within Terraform?

What was the error message?

Same error as before:

Call to function "element" failed: cannot use element function with an empty

My suggestion was to remove the call to the element function, so if you are still seeing the message that suggests that there is some other call to element elsewhere in the configuration that would need a similar update.