Convert existing VM to azure-spot

sgrIRL 6 Reputation points

I cannot find in the documentation a mention of converting an existing VM to an azure-spot instance.
Nor can I see a way in the portal to do so, although all the relevant azure-spot information fields are showing for the VM - they just show it isn't a spot!

Can a VM be converted?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,566 questions
Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
363 questions
{count} vote

6 answers

Sort by: Most helpful
  1. Larry Claman 26 Reputation points

    Seems like you could re-create the VM using the same technique as you would to move a VM into an availability set. (eg
    I'll see if I can prototype this & will post back when I have it working

    Edit: got this working. see

    3 people found this answer helpful.

  2. msrini-MSFT 9,271 Reputation points Microsoft Employee

    No, you can only set Spot pricing flag during the creation time. As of today you cannot convert the VM to Spot VM.

    2 people found this answer helpful.

  3. Ed Morris 36 Reputation points

    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 deallocate.
    2. Go 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.
    12. All other options are the same as using an on-demand instance.
    2 people found this answer helpful.
    0 comments No comments

  4. Derek Da Silva 1 Reputation point

    Larry- thanks for your script. I was able to use it to convert from spot to regular VM. Great job!

    0 comments No comments

  5. Aammir M B 0 Reputation points

    I have created the PS script to convert PYUG to SPOT VMs. It will not work for VMs with extensions as we will be recreating with existing disks.

    Function Convert-PYUG2SPOT {
        Param (
            # Set variables to your specifics
        $WarningPreference = 'SilentlyContinue'
        $context = Set-AzContext $subscriptionName
        Write-Output "Setting context to $($subscriptionName)"
        # Get the details of the VM to be moved to the Availability Set
        $originalVM = Get-AzVM `
            -ResourceGroupName $resourceGroup `
            -Name $vmName
        try {
            # Check whether already logged-in
            $setAzContext = (az account set --name $subscriptionName)
            $updateresource = (az resource update --resource-group $resourceGroup --name $vmName --resource-type virtualMachines --namespace Microsoft.Compute --set properties.storageProfile.osDisk.deleteOption=detach)
        catch {
            Write-Host 'You need to login'
            if ($LASTEXITCODE -ne 0) { exit 1 }
        # checking for dependencies
        $vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
        $extensionExist = ($vm.Extensions | Select-Object Publisher, VirtualMachineExtensionType, TypeHandlerVersion)
        if ($extensionExist) {
            Write-Host 'Make sure you note the installed extensions, that you can configure later on the converted VM.'
            Write-Host 'Please re-run the automation with the -force switch to initiate the conversion.'
        if ($extensionExist -and $force) {
            Write-Host 'Initiating the conversion.'
            # Create the basic configuration for the replacement VM.
            $newVM = New-AzVMConfig `
                -VMName $originalVM.Name `
                -VMSize $originalVM.HardwareProfile.VmSize `
                -Priority 'Spot' -MaxPrice -1
            # Confgure OS Disk
            Set-AzVMOSDisk `
                -VM $newVM -CreateOption Attach `
                -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `
                -Name $originalVM.StorageProfile.OsDisk.Name
            if ($originalVM.OSProfile.WindowsConfiguration) {
                $newVM.StorageProfile.OsDisk.OsType = 'Windows'
            else {
                $newVM.StorageProfile.OsDisk.OsType = 'Linux'
            # Taking backup of OSdisks
            $osDiskid = $originalVM.StorageProfile.OsDisk.ManagedDisk.Id
            $osDiskName = $originalVM.StorageProfile.OsDisk.Name
            # Create Disk Snapshot
            $snapshot = New-AzSnapshotConfig -SourceUri $osDiskid -Location $originalVM.Location -CreateOption copy
            $newsnap = New-AzSnapshot `
                -Snapshot $snapshot `
                -SnapshotName "$($osDiskName)_snapshot" `
                -ResourceGroupName $resourceGroup
            $randomNumber = Get-Random -Maximum 100
            foreach ($disk in $originalVM.StorageProfile.DataDisks) {
                # Taking backup of disks
                # Get Current VM Data Disk metadata.
                $dataDiskid = $disk.ManagedDisk.Id
                $dataDiskName = ($disk.Name).ToLower()
                # Create Disk Snapshot
                $snapshot = New-AzSnapshotConfig -SourceUri $dataDiskid -Location $originalVM.Location -CreateOption copy
                $newsnap = New-AzSnapshot `
                    -Snapshot $snapshot `
                    -SnapshotName "$($dataDiskName)_$($randomNumber)" `
                    -ResourceGroupName $resourceGroup
            # Add NIC(s) and keep the same NIC as primary
            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
            if ($originalVM.AvailabilitySetReference.Id) {
                Write-Output "Warning: VM $originalVM.Name is in an availability set. Spot VMs cannot run in availability sets."
            # Remove the original VM
            Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
            New-AzVM `
                -ResourceGroupName $resourceGroup `
                -Location $originalVM.Location `
                -VM $newVM `
            foreach ($attachDisk in $originalVM.StorageProfile.DataDisks) {
                $dataDiskid = ($attachDisk.ManagedDisk.Id).ToLower()
                $dataDiskName = ($attachDisk.Name).ToLower()
                Write-Output "Attaching $($dataDiskName)"
                Write-Output $dataDiskid
                Write-Output $dataDiskName
                $vm = Get-AzVM -Name $vmName -ResourceGroupName $resourceGroup
                $vm = Add-AzVMDataDisk -VM $vm `
                    -Name $dataDiskName `
                    -ManagedDiskId $dataDiskid `
                    -Lun $attachDisk.Lun `
                    -CreateOption Attach
                Write-Output " --- Adding data disk $($dataDiskName) to $($vmName)"
                Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
    0 comments No comments