Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Azure Spot VMs Explained

·906 words·5 mins· 100 views · 5 likes ·
Add-AzVMNetworkInterface Azure PowerShell Connect-AzAccount Get-AzSubscription

Hello, I want to talk to you about Azure Spot virtual machines. Although this functionality is currently in a public preview, I find it an exciting alternative to save money in dev/test environments but not in production environments. And so in this article, I will show you how to implement this type of virtual machine using Azure PowerShell.

What Microsoft says about Spot VMs: Using Azure Spot VMs allows you to take advantage of our unused capacity at a significant cost savings. At any point in time when Azure needs the capacity back, the Azure infrastructure will evict Spot VMs. VMs can be evicted based on capacity or the max price you set. For virtual machines, the eviction policy is set to Deallocate which moves your evicted VMs to the stopped-deallocated state, allowing you to redeploy the evicted VMs later.

Important characteristics of Azure Spot VMs are:>
Important characteristics of Azure Spot VMs are: #
  • Azure does not offer SLA for this type of VM.
  • If Azure needs the capacity for “pay as you go” workloads, Azure’s infrastructure will evict the Spot virtual machines with a 30-second warning.
  • VMs can be evicted based on capacity or the max price you set.
  • The amount of available capacity can vary based on size, region, or time of day.
Features not compatible with ephemeral disks at the time of publication of this article:>
Features not compatible with ephemeral disks at the time of publication of this article: #
  • B-series.
  • Promo versions of any size (like Dv2, NV, NC, and H promo sizes).
  • Ephemeral OS disks.
  • Spot VMs cannot be deployed to Microsoft Azure China 21Vianet and the Department of Defense (DoD) in the Azure Government region.

Once explained the characteristics of the Azure Spot VM we can begin this tutorial.


This tutorial assumes that you already have a Microsoft Azure account set up and you have the PowerShell Azure module on your machine installed. If you want to know how to install the PowerShell Azure module on your machine, check out this link. Assumptions:

  • You created a Resource Group for these resources and the new ones deployed in this tutorial will join that group. If you want to know how to create a Resource Group, check out this link.
  • You already created the necessary Virtual Network and subnet. If you want to know how to create a Virtual Network, check out this link.

The simplest way to get started is to sign in interactively at the command line.


This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Select-AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you’re ready to start.

Set the variables>

Set the variables #

Here, we define the characteristics of our environment and the resource’s properties.

#Define the parameters for the Azure resources.

$location = "northeurope"
$ResourceGroupName = "RG-DEMO-NE"

#Define the parameters for the virtual machine.

$computerName = "VM-DEMO-SPOT"
$vmSize = "Standard_DS1"
$publisherName = "MicrosoftWindowsServer"
$offer = "WindowsServer"
$skus = "2019-Datacenter"

#Define the existing VNet information.

$networkName = "DEMO-VNET"
$nicName = "NIC-"+$computerName
$vnet = Get-AzVirtualNetwork `
   -Name $NetworkName `
   -ResourceGroupName $ResourceGroupName

#Gets a credential object based on a username and password.

$credential = Get-Credential
Set the Spot VM properties>

Set the Spot VM properties #

To use an Azure Spot virtual machine, you must use the New-AzVMConfig cmdlet with the following syntax.

$VirtualMachine = New-AzVMConfig `
   -VMName $ComputerName `
   -VMSize $VMSize `
   -Priority "Spot" `
   -MaxPrice -1

-Priority: Specify the priority for the virtual machine. The only supported values are ‘Regular’, ‘Spot’, and ‘Low’.’ Low’ is deprecated instead used ‘Spot’. -MaxPrice: Specify a max price you are willing to pay, per hour, for the VM in US dollars (USD), using up to 5 decimals. If you set the value to -1, the VM will not be evicted based on the price. Important: Because it is currently in the public preview, Azure infrastructure will only carry out the eviction of the Spot VMs based on capacity. You can set a maximum price, but it will be ignored. VMs will have a fixed price.

Azure Resource Deployment>

Azure Resource Deployment #

In this section, you create and deploy the virtual machine in Microsoft Azure with the features and properties previously declared.

#Create the Network Interface.

$NIC = New-AzNetworkInterface `
   -Name $nicName `
   -ResourceGroupName $ResourceGroupName `
   -Location $location `
   -SubnetId $Vnet.Subnets[0].Id

#Define the parameters for the new virtual machine.

$VirtualMachine = Set-AzVMOperatingSystem `
   -VM $VirtualMachine `
   -Windows `
   -ComputerName $ComputerName `
   -Credential $Credential `
   -ProvisionVMAgent  `

$VirtualMachine = Add-AzVMNetworkInterface `
   -VM $VirtualMachine `
   -Id $NIC.Id

$VirtualMachine = Set-AzVMSourceImage `
   -VM $VirtualMachine `
   -PublisherName $publisherName `
   -Offer $offer `
   -Skus $skus `
   -Version latest
Virtual Machine Deployment>

Virtual Machine Deployment #

finally, we perform the deployment of the virtual machine, using the New-AzVM cmdlet with the following syntax:

New-AzVM `
   -ResourceGroupName $ResourceGroupName `
   -Location $location `
   -VM $VirtualMachine `
Verify the deployment>

Verify the deployment #

You can use the following command to verify the deployment.

Get-AzVM `
   -ResourceGroupName $ResourceGroupName `
   | Select-Object Name,Location,ProvisioningState,Priority,@{Name="maxPrice"; Expression={$_.BillingProfile.MaxPrice}}

Azure Spot VMs
When these VMs are in general available, I will write a new article about them. If you want to know more about Azure Spot VMs, check out this link.