Empty tuple error while creating azure public IP address


I was trying to provision multiple VM with public IP address. But am getting below error while using “count.index”. Also I noticed that in my variable.tf file if addd the vmcount = 2 then it will work, if I change the variable vmcount = 1 then it is failing.


    $ terraform --version
Terraform v0.12.12
+ provider.azurerm v1.30.0

Terraform code:

variable.tf file

    variable "vmcount" {
        type        = number
        default     = 1

network.tf file

resource "azurerm_public_ip" "vmip" {
    count                  = var.vmcount
    name                   = "${var.name}ip-${count.index}"
    resource_group_name    = var.rgname
    allocation_method      = "Static"
    location               = var.location

resource "azurerm_network_interface" "terranic" {
count                  = var.vmcount
name                   = "${var.name}nic-${count.index}"
location               = var.location
resource_group_name    =  var.rgname

ip_configuration {
    name                          = "${var.name}ip-config-${count.index}"
    subnet_id                     = "${azurerm_subnet.vmsubnet.id}"
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id           = azurerm_public_ip.vmip["${count.index}"].id

When I execute terrafor plan getting below error.

Error: Invalid index

  on network.tf line 83, in resource "azurerm_network_interface" "terranic":
  83:         public_ip_address_id           = azurerm_public_ip.vmip["${count.index}"].id
    | azurerm_public_ip.vmip is empty tuple
    | count.index is 0

The given key does not identify an element in this collection value.

I have modified my code multiple times as follows.

public_ip_address_id = "${azurerm_public_ip.vmip..id}"
public_ip_address_id = [azurerm_public_ip.vmip[count.index].id]
public_ip_address_id = element(azurerm_public_ip.vmip.
.id, count.index)

Any help will be much appreciated.


I noticed that * is not displaying in my code which I added here. Please consider * between two dots (…)