Convert an Azure VM to a Spot VM type

Convert an Azure VM to a Spot VM type

Azure Spot Virtual Machines allows you to take advantage of unused capacity in Azure Data Centre at a significant reduced cost thus giving you some cost savings. At any point in time when Azure needs the capacity back, the Azure infrastructure will evict Azure Spot Virtual Machines. Therefore, Azure Spot Virtual Machines are great for workloads that can handle interruptions like batch processing jobs, dev/test environments, large compute workloads, and more.

Use the below Powershell script to convert an Azure VM to a Spot VM type:

# Set variables

# Get the details of the VM to be converted to SpotVM
$originalVM = Get-AzVM -ResourceGroupName $rg -VMName $vm

# Create the basic configuration for the replacement VM
$newVM = New-AzVMConfig -VMName $originalVM.Name `
                        -VMSize $originalVM.HardwareProfile.VmSize`
                        -Priority "Spot" -MaxPrice -1

  Set-AzVMOSDisk `
       -VM $newVM -CreateOption Attach `
       -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `
       -Name $originalVM.StorageProfile.OsDisk.Name `
  Set-AzVMOSDisk `
       -VM $newVM -CreateOption Attach `
       -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `
       -Name $originalVM.StorageProfile.OsDisk.Name `

# Add Data Disks
foreach ($disk in $originalVM.StorageProfile.DataDisks) { 
    Add-AzVMDataDisk -VM $newVM `
       -Name $disk.Name `
       -ManagedDiskId $disk.ManagedDisk.Id `
       -Caching $disk.Caching `
       -Lun $disk.Lun `
       -DiskSizeInGB $disk.DiskSizeGB `
       -CreateOption Attach

# Add NIC(s) and keep the same NIC as primary; 
# keep the Private IP too, if it exists. 
foreach ($nic in $originalVM.NetworkProfile.NetworkInterfaces) {    
    if ($nic.Primary -eq "True")
            Add-AzVMNetworkInterface `
               -VM $newVM `
               -Id $nic.Id -Primary
      else {
                 Add-AzVMNetworkInterface `
                -VM $newVM `
                 -Id $nic.Id 

# Recreate the VM
New-AzVM `
       -ResourceGroupName $rg `
       -Location $originalVM.Location `
       -VM $newVM `

You can also achieve the same through the web portal.

The steps are:

  1. Power down your VM from the VM summary page and wait for it to de-allocate.
  2. Go to DISK page of the VM, select the link for the OS disk to look at its Overview.
  3. On the overview page, click the create snapshot button to create an image of the current OS disk.
  4. Give it a name, and make the snapshot type full, selecting also Zone-redundant.
  5. Create the snapshot, taking note of its size.
  6. Navigate to the disk page from the Azure portal homepage, and in the top left of the menu bar select the add button.
  7. Create the disk as you would like, making sure the disk size is at least the size of the snapshot (it can be larger if you like). Select the source type as snapshot and select the name of the snapshot you just created.
  8. Create the disk.
  9. Navigate back to the Disks list page on the portal, and click into the new disk just created to view the overview page.
  10. On the new disks overview page, select Create VM from the horizontal menu bar, which will start a VM creation page, with the current disk as the OS disk.
  11. Be sure to select the Azure Spot Instance radio button to yes to make this a spot instance.

All other options are the same as using an on-demand instance.