Create, then modify a resource in order?

Hi there, new to terraform, so apologies if this is covered elsewhere (a search didn’t turn up anything, but I might not know the right syntax).

I have a resource (VM) in Azure that I’m trying to create, but it fails due to a bug in Azure if it’s created with >1 NIC. However, if created with 1 NIC, then it’s fine.

So, to work around the bug, I need to create the VM with the 1 NIC, then add the second NIC after it’s created. I know this works in Azure and works around the bug.

If I create a terraform resource with the 1 nic, trun the apply, then update the configuration file, and run the apply the second time, it all works great!

However, I can’t work out a way to get terraform to do this without me modifying the terraform file in between runs (which rather runs counter to my automation strategy!).

Relevant code, if it helps:

resource “azurerm_linux_virtual_machine” “edgeconnectvm” {

name = local.edgeconnect_vmname

location = data.azurerm_resource_group.edgeconnect_resource_group.location

resource_group_name = data.azurerm_resource_group.edgeconnect_resource_group.name

availability_set_id = azurerm_availability_set.example.id

size = “Standard_D2s_v3”

admin_username = “”

network_interface_ids = [

azurerm_network_interface.WAN_NIC.id,

azurerm_network_interface.VNET_NIC.id,

]

os_disk {

caching              = "ReadWrite"

storage_account_type = "Premium_LRS"

}

source_image_reference {

publisher = local.publisher

offer     = local.product

sku       = local.edgeconnect_sku

version   = local.edgeconnect_version

}

plan {

publisher = local.publisher

product   = local.product

name      = local.edgeconnect_sku

}

boot_diagnostics {

storage_account_uri = azurerm_storage_account.diags.primary_blob_endpoint

}

Hi @paul-haigh,

This sort of thing is not really compatible with Terraform’s declarative model: you want to tell Terraform exactly which sequence of operations to take, rather than to tell Terraform your desired state and let it choose the smallest number of steps to get there.

In the Terraform architecture it’s generally the provider’s responsibility to decide which actions need to be taken to transition from one state to another. I’m not familar with Azure but your framing of this being an Azure bug makes it seem like something everyone using Azure would encounter, in which case you might like to see if there’s already some discussion about this problem in the Azure provider repository.

If this limitation you’re describing is one that would affect anyone trying to create a VM with more than one NIC, the provider development team may be interested in adding some additional logic to the resource type in question to get the correct sequence of events without the need to use Terraform in a non-standard way.

1 Like

Thank you for the response.

Totally understand the declarative model, if it worked for my situation, I’d use it :slight_smile:
Sadly, this is a bug in a specific VM image, not a general one, so I suspect it may never get fixed, which is a shame, so leaves me a bit stuck in resolving it, as it’s to do with the image specifically.

Thanks again for the response.

Hi @paul-haigh,

I’m sorry it does sound like you’re a bit stuck there. My limited experience with Azure in particular means I can’t offer any specific advice on working around this if fixing the issues in the image isn’t an option.

We do have a specific forum category for the Azure provider, so moving or re-asking this question there might make it visible to folks with more Azure experience.