Need to start 2 time 'terraform plan' and 'terraform apply' to create subnets on Azure

Hello,

I have a problem when Terraform tried to create subnets on a virtual network on Azure.
The first time I run it, it can’t create them. But it always succeeds the second time. I don’t understand why.

On my file ‘variable.tf’, I have :

 variable "VnetAddress" {
    type = list(string)
    default = ["172.28.0.0/16"]
}
 locals {
    SubnetNameArr = "sne01${local.VnetName}"
    SubnetAddressArr = "172.28.1.0/24"
    SubnetNameDef = "sne03${local.VnetName}"
    SubnetAddressDef = "172.28.3.0/24"
    SubnetNameAut = "sne05${local.VnetName}"
    SubnetAddressAut = "172.28.5.0/24"
    SubnetNameApp = "sne06${local.VnetName}"
    SubnetAddressApp = "172.28.6.0/24"
    SubnetNameAdm = "sne04${local.VnetName}"
    SubnetAddressAdm = "172.28.4.0/24"
    SubnetNameRmq = "sne02${local.VnetName}"
    SubnetAddressRmq = "172.28.2.0/24"
    SubnetNameAtm = "sne10${local.VnetName}"
    SubnetAddressAtm = "172.28.10.0/24"
    SubnetNameAks = "sne07${local.VnetName}"
    SubnetAddressAks = "172.28.7.0/24"
    SubnetNameCdd = "sne99${local.VnetName}"
    SubnetAddressCdd = "172.28.99.0/24"
}
 locals {
  subnetNames = [local.SubnetNameArr, local.SubnetNameRmq, local.SubnetNameDef, local.SubnetNameAtm, local.SubnetNameAut, local.SubnetNameApp, local.SubnetNameAdm, local.SubnetNameAks, local.SubnetNameCdd]
  subnetAddress = [local.SubnetAddressArr, local.SubnetAddressRmq, local.SubnetAddressDef, local.SubnetAddressAtm, local.SubnetAddressAut, local.SubnetAddressApp, local.SubnetAddressAdm, local.SubnetAddressAks, local.SubnetAddressCdd]
}

On an other file named ‘VirtualNetwork.tf’, I have :

  resource "azurerm_virtual_network" "Terra-VN" { 
    name = local.VnetName 
    address_space = var.VnetAddress
    location = var.AzureRegion
    resource_group_name = azurerm_resource_group.Terra-RG.name
  tags = local.tags
}  
 resource "azurerm_subnet" "Terra-Subnet" {
  count = length(local.subnetNames)
  name = element(local.subnetNames, count.index)
  resource_group_name = azurerm_resource_group.Terra-RG.name
  virtual_network_name = azurerm_virtual_network.Terra-VN.name
  address_prefixes     = [element(local.subnetAddress, count.index)]
  provisioner "local-exec" {
    command = "sleep 20"
    interpreter = ["PowerShell", "-Command"]
  }
}

I have this output :

 output "subnet_id" {
  value = [element(tolist(azurerm_virtual_network.Terra-VN.subnet), 0).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 1).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 2).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 3).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 4).id,
  element(tolist(azurerm_virtual_network.Terra-VN.subnet), 5).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 6).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 7).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 8).id]
}

The first time I execute ‘terraform plan’ :

 Changes to Outputs:
      + subnet_id = [
      + (known after apply),
      + (known after apply),
      + (known after apply),
      + (known after apply),
      + (known after apply),
      + (known after apply),
      + (known after apply),
      + (known after apply),
      + (known after apply),
    ]

The result is :

│ Error: Error in function call
│
│   on Variables.tf line 96, in output "subnet_id":
│   96:   value = [element(tolist(azurerm_virtual_network.Terra-VN.subnet), 0).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 1).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 2).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 3).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 4).id,
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 96, in output "subnet_id":
│   96:   value = [element(tolist(azurerm_virtual_network.Terra-VN.subnet), 0).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 1).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 2).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 3).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 4).id,
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 96, in output "subnet_id":
│   96:   value = [element(tolist(azurerm_virtual_network.Terra-VN.subnet), 0).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 1).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 2).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 3).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 4).id,
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 96, in output "subnet_id":
│   96:   value = [element(tolist(azurerm_virtual_network.Terra-VN.subnet), 0).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 1).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 2).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 3).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 4).id,
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 96, in output "subnet_id":
│   96:   value = [element(tolist(azurerm_virtual_network.Terra-VN.subnet), 0).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 1).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 2).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 3).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 4).id,
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 97, in output "subnet_id":
│   97:   element(tolist(azurerm_virtual_network.Terra-VN.subnet), 5).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 6).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 7).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 8).id]
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 97, in output "subnet_id":
│   97:   element(tolist(azurerm_virtual_network.Terra-VN.subnet), 5).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 6).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 7).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 8).id]
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 97, in output "subnet_id":
│   97:   element(tolist(azurerm_virtual_network.Terra-VN.subnet), 5).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 6).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 7).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 8).id]
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 97, in output "subnet_id":
│   97:   element(tolist(azurerm_virtual_network.Terra-VN.subnet), 5).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 6).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 7).id, element(tolist(azurerm_virtual_network.Terra-VN.subnet), 8).id]
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 104, in locals:
│  104:     SubnetIdsArr = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 0).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 117, in locals:
│  117:     SubnetIdsRmq = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 2).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 130, in locals:
│  130:     SubnetIdsAtm = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 1).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 143, in locals:
│  143:     SubnetIdsDef = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 3).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 155, in locals:
│  155:     SubnetIdsAdm = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 4).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 169, in locals:
│  169:     SubnetIdsAut = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 5).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 182, in locals:
│  182:     SubnetIdsApp = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 6).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.
╵
╷
│ Error: Error in function call
│
│   on Variables.tf line 221, in locals:
│  221:   SubnetIdsCdd = element(tolist(azurerm_virtual_network.Terra-VN.subnet), 8).id
│     ├────────────────
│     │ azurerm_virtual_network.Terra-VN.subnet is empty set of object
│
│ Call to function "element" failed: cannot use element function with an empty list.

After the first try, I need to do another ‘terraform plan’ to made a ‘terraform apply’.
The second plan gave me this output :

Changes to Outputs:
  + subnet_id = [
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne01XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne10XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne02XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne03XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne04XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne05XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne06XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne07XXX",
      + "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/rgo01XXX/providers/Microsoft.Network/virtualNetworks/vne01XXX/subnets/sne99XXX",
    ]

After executed the second time, Terraform succeeded in creating all Azure ressources.

Someone have an idea to resolve the issue ?
Thank you so much.

Issue here is with your output. Instead of using azurerm_virtual_network.Terra-VN.subnet in the output try and use azurerm_subnet.Terra-Subnet[index].id.

Event though azurerm_virtual_network has a list of subnets in the resource, you are creating them using azurerm_subnet, unless you refresh azurerm_virtual_network, the state file won’t have the subnet information in there.

And this is why when you run it second time you are getting the output right.

1 Like

Thanks @ramsateesh
That was exactly right.

1 Like