Azure Spot VMs for cost savings - Commands & Configuration
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vmName": {
"type": "string",
"defaultValue": "spot-vm-example",
"metadata": {
"description": "Name of the virtual machine"
}
},
"adminUsername": {
"type": "string",
"defaultValue": "azureuser",
"metadata": {
"description": "Admin username for the VM"
}
},
"adminPassword": {
"type": "securestring",
"metadata": {
"description": "Admin password for the VM"
}
}
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2022-08-01",
"name": "[parameters('vmName')]",
"location": "eastus",
"properties": {
"priority": "Spot",
"evictionPolicy": "Deallocate",
"billingProfile": {
"maxPrice": -1
},
"hardwareProfile": {
"vmSize": "Standard_DS1_v2"
},
"storageProfile": {
"imageReference": {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "18.04-LTS",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
}
},
"osProfile": {
"computerName": "[parameters('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('vmName'), '-nic'))]"
}
]
}
}
},
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2022-07-01",
"name": "[concat(parameters('vmName'), '-nic')]",
"location": "eastus",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/default"
},
"privateIPAllocationMethod": "Dynamic"
}
}
]
}
}
]
}This ARM template creates an Azure Spot VM named 'spot-vm-example' in the East US region.
priority: Set to 'Spot' to use spare capacity at a lower cost.
evictionPolicy: 'Deallocate' means the VM is stopped but not deleted if Azure reclaims it.
billingProfile.maxPrice: -1 means pay up to the regular price, but usually cheaper.
The VM uses Ubuntu 18.04 LTS and a standard DS1_v2 size.
A network interface is created and attached to the VM in an existing virtual network subnet.
az group create --name myResourceGroup --location eastus
--name - Sets the name of the resource group--location - Sets the Azure region for the resource groupaz deployment group create --resource-group myResourceGroup --template-file azuredeploy.json --parameters adminPassword=MyStrongP@ssw0rd
--resource-group - Specifies the resource group to deploy into--template-file - Points to the ARM template file--parameters - Passes parameters like admin password securelyaz vm get-instance-view --resource-group myResourceGroup --name spot-vm-example --query instanceView.statuses[1]--resource-group - Specifies the resource group of the VM--name - Specifies the VM name--query - Filters output to show VM power stateIf you remember nothing else from this pattern, remember: Azure Spot VMs let you save money by using spare capacity that can be taken back anytime, so use them only for flexible, interruptible workloads.
Practice
Solution
Step 1: Understand Azure Spot VM purpose
Azure Spot VMs use spare capacity in Azure data centers, which is cheaper but can be interrupted.Step 2: Identify the main benefit
The main benefit is cost savings by using this spare capacity, not guaranteed uptime or unlimited storage.Final Answer:
They offer significant cost savings by using unused capacity. -> Option BQuick Check:
Spot VMs = Cost savings [OK]
- Thinking Spot VMs guarantee uptime
- Confusing Spot VMs with premium storage
- Assuming Spot VMs auto-scale without setup
Solution
Step 1: Recall Azure CLI syntax for Spot VMs
To create a Spot VM, the correct option is--priority Spot.Step 2: Eliminate incorrect options
--priority Standardis for regular VMs;--spot-enabledand--vm-typeare not valid Azure CLI options.Final Answer:
--priority Spot -> Option AQuick Check:
Spot VM creation uses --priority Spot [OK]
- Using --priority Standard instead of Spot
- Assuming --spot-enabled is valid
- Confusing VM type options
Solution
Step 1: Understand max price behavior
The max price sets the highest price you agree to pay for the Spot VM.Step 2: Analyze what happens if max price is too low
If the current Spot price is higher than your max price, Azure will not create the VM to avoid exceeding your limit.Final Answer:
The VM creation will fail due to price limit. -> Option CQuick Check:
Max price below Spot price = VM creation fails [OK]
- Thinking VM runs at max price anyway
- Assuming VM downgrades performance
- Believing VM converts to standard automatically
Solution
Step 1: Understand Spot VM eviction reasons
Spot VMs can be evicted when Azure reclaims capacity for higher priority workloads.Step 2: Identify the most common eviction cause
The most common cause is Azure needing the capacity back, not disk space or manual deletion.Final Answer:
Azure needed the capacity back for other workloads. -> Option DQuick Check:
Spot VM eviction = Azure reclaims capacity [OK]
- Assuming eviction is due to disk space
- Thinking eviction happens if max price exceeded
- Believing eviction is always manual
Solution
Step 1: Match workload needs to Spot VM features
Batch jobs that can be interrupted fit Spot VMs well because they save costs but may be evicted.Step 2: Choose best configuration for cost and reliability
Setting a max price controls cost, and handling eviction automatically ensures job completion despite interruptions.Step 3: Eliminate other options
Standard VMs cost more; no max price risks failures; Azure Functions are different service types.Final Answer:
Create Spot VMs with a max price set to your budget and use automatic eviction handling. -> Option AQuick Check:
Spot VMs + max price + eviction handling = cost-effective batch jobs [OK]
- Not setting max price and risking high costs
- Choosing standard VMs for interruptible jobs
- Confusing Azure Functions with Spot VMs
Ready to prove it?
Pass the test and this pattern moves unknown
