Harsha G V
Harsha G V

Reputation: 1030

How to dissociate a static public IP+network interface from a VM in Azure?

How to dissociate a static public IP/network interface or both of it from a VM and attach it to another VM using terraform in MS Azure just like an Elastic IP in AWS?

If azure terraform does not works, azure CLI is also feasible.

I was only able to find a way to detach an IP using azure CLI but could not be able to attach it to a new VM, due to an error - NicInUse.

 az network nic ip-config update \
 --name ipconfigmyVM \
 --resource-group myResourceGroup \
 --nic-name myVMVMNic \
 --remove PublicIpAddress

Upvotes: 2

Views: 1605

Answers (1)

Nancy Xiong
Nancy Xiong

Reputation: 28284

After you detach a public Ip address as the above commands in your question, you can attach this disassociated Public Ip address to a new nic(without public Ip address). Due to the Nic conflicting error, you could verify if you are trying to attaching a public Ip address that is being in use with another NIC.

 az network nic ip-config update \
 --name ipconfigmyVM \
 --resource-group myResourceGroup \
 --nic-name myVMVMNic \
 --public-ip-address existingPublicIpAddress

enter image description here

Please note that the new public Ip address should have the same region as the NIc when you attach the new Public IP to the old Azure Nic. If the resources are not in the same resource group, you could use the resource ID of the Public IP address.

--public-ip-address "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/publicIPAddresses/test-pip"

With Terraform, you can associate Public IP Address with this NIC via using public_ip_address_id in the resource "azurerm_network_interface".

Upvotes: 1

Related Questions