pulumi / pulumi-azure

A Microsoft Azure Pulumi resource package, providing multi-language access to Azure
Apache License 2.0
133 stars 50 forks source link

VM replacement failed due to network interface attached to current VM #811

Open bellaxievmlyr opened 3 years ago

bellaxievmlyr commented 3 years ago

Hi there,

I'm facing this issue that seems is a bug, asked the question in slack pulumi-azure channel, was advised to create a ticket here.

This is pulumi-python to create VM and network interface, then change custom_data of the VM which result in replacement of the resource. Pulumi up returned the following error: azure:compute:LinuxVirtualMachine (WebVM): error: creating Linux Virtual Machine "xxxx" (Resource Group "xxxx"): compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="NicInUse" Message="Network Interface networkinterface1 is used by existing resource /subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Compute/virtualMachines/xxxx. In order to delete the network interface, it must be dissociated from the resource.

As both the VM and the NIC are created thru the same pulumi script, I expected pulumi to handle the dependency gracefully to first delete the old VM which would dissociate the NIC and then create the new VM which is to link with the NIC. However, it looks like pulumi tried to create the new VM before deleting the old VM, hence resulted the error above.

Code snippet as following:

network_interface = azure.network.NetworkInterface("NetworkInterface", location=az_location, resource_group_name=resource_group_name, ip_configurations=[azure.network.NetworkInterfaceIpConfigurationArgs( name="{}-{}-nic".format(project, env), subnet_id=app_subnet_id, private_ip_address_allocation="Dynamic", public_ip_address_id=public_ip_vm_id, )] )

`web_vm = azure.compute.LinuxVirtualMachine("WebVM", location=az_location, resource_group_name=resource_group_name, network_interface_ids=[network_interface.id], size=config.require('vm_size'), source_image_reference=azure.compute.VirtualMachineStorageImageReferenceArgs( publisher="Canonical", offer="UbuntuServer", sku=config.require('os_version'), version=config.require("vm_ubuntu_ver"), ), os_disk=azure.compute.LinuxVirtualMachineOsDiskArgs(

name="{}-{}-osDisk".format(project, env),

    caching="ReadWrite",
    #create_option="FromImage",
    storage_account_type="StandardSSD_LRS",
),
admin_username=config.require('vm_admin'),
computer_name="{}-{}".format(project, env),
disable_password_authentication=True,
admin_ssh_keys=[azure.compute.LinuxVirtualMachineAdminSshKeyArgs(
    public_key=config.require('ssh_pub_key'),
    username=config.require('vm_admin')
)],
custom_data=databytes,
identity=azure.compute.LinuxVirtualMachineIdentityArgs(
    type="UserAssigned",
    identity_ids=[identity_details["id"]]
))`
mikhailshilkov commented 3 years ago

Thank you for reporting this. As a workaround, you should be able to set the deleteBeforeReplace option for the VM to get this behavior.