Deploy Spot VMs using the Azure CLI

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. Therefore, Spot VMs are great for workloads that can handle interruptions like batch processing jobs, dev/test environments, large compute workloads, and more.

Pricing for Spot VMs is variable, based on region and SKU. For more information, see VM pricing for Linux and Windows.

You have option to set a max price you are willing to pay, per hour, for the VM. The max price for a Spot VM can be set in US dollars (USD), using up to 5 decimal places. For example, the value 0.98765would be a max price of $0.98765 USD per hour. If you set the max price to be -1, the VM won't be evicted based on price. The price for the VM will be the current price for Spot or the price for a standard VM, which ever is less, as long as there is capacity and quota available. For more information about setting the max price, see Spot VMs - Pricing.

The process to create a VM with Spot using the Azure CLI is the same as detailed in the quickstart article. Just add the '--priority Spot' parameter, set the --eviction-policy to either Deallocate (this is the default) or Delete, and provide a max price or -1.

Install Azure CLI

To create Spot VMs, you need to be running the Azure CLI version 2.0.74 or later. Run az --version to find the version. If you need to install or upgrade, see Install the Azure CLI.

Sign in to Azure using az login.

az login

Create a Spot VM

This example shows how to deploy a Linux Spot VM that will not be evicted based on price. The eviction policy is set to deallocate the VM, so that it can be restarted at a later time. If you want to delete the VM and the underlying disk when the VM is evicted, set --eviction-policy to Delete.

az group create -n mySpotGroup -l eastus
az vm create \
    --resource-group mySpotGroup \
    --name myVM \
    --image UbuntuLTS \
    --admin-username azureuser \
    --generate-ssh-keys \
    --priority Spot \
    --max-price -1 \
	--eviction-policy Deallocate

After the VM is created, you can query to see the max billing price for all of the VMs in the resource group.

az vm list \
   -g mySpotGroup \
   --query '[].{Name:name, MaxPrice:billingProfile.maxPrice}' \
   --output table

Next steps

You can also create a Spot VM using Azure PowerShell, portal, or a template.

If you encounter an error, see Error codes.