0
votes

Use-case: Create X amount of VM’s with a public ip assigned to each VM.

Research so far has found a previous issue ticket created - dated 2017. (https://github.com/hashicorp/terraform/issues/15285) where @apparentlymart discussed this issue.

The fix was that in terraform v0.12 explicit support was added for individual constants being added for reference in a depends_on.

My Repo: https://github.com/CPWu/terraform_azure_compute

Attempting to create a module that will create X number of azure linux computes, the first VM is created perfectly but the second compute unit on a “2X” request does not get created properly. The dynamic IP is created but does not get assigned to the NIC that’s associated to the second VM. My understanding is that because the count’ed’ resources are being seen as a single node and the IP is not available at time of NIC creation. Looked into the for_each construct implemented in v0.12.6 but that doesn’t look like that will solve my issue.

Update: I can see that all the resources are created but IP[1] and so moving forward do not get the association to the respective server NIC. I also posted on Hashicorp community forums with no response yet.

1

1 Answers

0
votes

Quick idea,

Try to change the depends on in here:

resource "azurerm_network_interface" "sandbox_nic" {
name                                = "${var.SERVER_NAME}-${format("%02d",count.index)}-nic"
location                            = var.AZURE_REGION
resource_group_name                 = var.RESOURCE_GROUP_NAME
count                               = var.NODE_COUNT

ip_configuration {
    name                                      = "${var.SERVER_NAME}-ip"  
    subnet_id                                 = azurerm_subnet.sandbox_subnet.id
    private_ip_address_allocation             = "dynamic"
    public_ip_address_id                      = count.index == 0 ? element(azurerm_public_ip.sandbox_public_ip.*.id,count.index) : null    
}

**depends_on = [
    azurerm_public_ip.sandbox_public_ip[1],
]**

}

To:

azurerm_public_ip.sandbox_public_ip without the index pointer The interface might get created faster than the 3rd and 4th IP thus not picking it up