In our Azure test account I regularly delete and recreate VM’s. But there are also VM’s that are used for longer period. During the day I was cleaning up a VM that I installed for a test and accidentally deleted my main demo firewall VM. The attached disks, network interface were not deleted. The question became can I recover and recreate the VM with the same disks and public IP?
Here are 2 options I found:
- Azure Portal: From the Azure Portal you can open the os disk and use the create VM option in the overview. From there you receive a new blade like you would when creating a new VM. If you want to keep the same public IP you need to dissociate the public IP from the network interface that is still existing and delete the network interface. That way you can assign the static private IP to this new VM.
- Powershell: Using powershell you can more flexible and reuse the network interface as well as the disks. In the example you can see the different steps that I used to recreate the VM.
# Configure the following variables to get started
$location = "West Europe"
$resourceGroupName = "JVH11"
$vmName = "JVH11-VM-NGF"
$vmSize = "Standard_F1s"
$networkInterfaceName = "jvh11-vm-ngf331"
$osDiskName = "JVH11-VM-NGF_OsDisk_1_bc6c1d4a1cfb4e99a75e03dff618460d"
# Create a new vm object with the required size and name
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
# Assign the plan (Azure Marketplace image) of the original os disk
$vm.Plan = @{'name'= "byol"; 'publisher'= 'barracudanetworks'; 'product' = "barracuda-ng-firewall"}
# Retrieve and assign the still existing network interface and public ip
$ifc = Get-AzureRmNetworkInterface -Name $networkInterfaceName -ResourceGroup $resourceGroupName
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $ifc.id -ErrorAction Stop -Primary
# Retrieve and assign the still existing osdisk
$disk = Get-AzureRmDisk -DiskName $osDiskName -ResourceGroup $resourceGroupName
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $disk.id -CreateOption Attach -Linux
# Recreate the VM based on the above attributes
New-AzureRmVM -VM $vm -ResourceGroup $resourceGroupName -Location $location
You can find this script (Recover_VM.ps1) also on my github.
Happy recovery!
Be First to Comment