0
0
Azurecloud~5 mins

Spot VMs for cost savings in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Spot VMs let you use spare cloud capacity at a lower price. They can save money but might stop running if the cloud needs the resources back.
When running batch jobs that can pause and resume later without issues.
When testing applications that do not need guaranteed uptime.
When running flexible workloads that can handle interruptions.
When you want to reduce cloud costs for non-critical tasks.
When experimenting with cloud services without spending much.
Config File - spot-vm-template.json
spot-vm-template.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2021-07-01",
      "name": "mySpotVM",
      "location": "eastus",
      "properties": {
        "priority": "Spot",
        "evictionPolicy": "Deallocate",
        "hardwareProfile": {
          "vmSize": "Standard_DS1_v2"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "Canonical",
            "offer": "UbuntuServer",
            "sku": "18.04-LTS",
            "version": "latest"
          },
          "osDisk": {
            "createOption": "FromImage"
          }
        },
        "osProfile": {
          "computerName": "mySpotVM",
          "adminUsername": "azureuser",
          "adminPassword": "P@ssw0rd1234!"
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic"
            }
          ]
        }
      }
    }
  ]
}

This JSON template creates a Spot VM named mySpotVM in the eastus region.

The priority is set to Spot to use spare capacity at a lower cost.

The evictionPolicy is Deallocate, meaning the VM is stopped but not deleted if the cloud reclaims the resources.

The VM size is Standard_DS1_v2, running Ubuntu 18.04 LTS.

It uses a network interface myNic which must exist in the specified resource group.

Commands
Create a resource group named myResourceGroup in the eastus region to hold your VM and related resources.
Terminal
az group create --name myResourceGroup --location eastus
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup", "location": "eastus", "managedBy": null, "name": "myResourceGroup", "properties": { "provisioningState": "Succeeded" }, "tags": {}, "type": "Microsoft.Resources/resourceGroups" }
--name - Specifies the resource group name
--location - Specifies the Azure region
Create a network interface named myNic in the resource group. This connects the VM to the network.
Terminal
az network nic create --resource-group myResourceGroup --name myNic --vnet-name myVnet --subnet mySubnet
Expected OutputExpected
{ "NewNIC": { "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic", "location": "eastus", "name": "myNic", "provisioningState": "Succeeded" } }
--resource-group - Specifies the resource group
--name - Names the network interface
--vnet-name - Specifies the virtual network name
--subnet - Specifies the subnet name
Deploy the Spot VM using the JSON template file to the resource group.
Terminal
az deployment group create --resource-group myResourceGroup --template-file spot-vm-template.json
Expected OutputExpected
{ "properties": { "provisioningState": "Succeeded", "outputs": {} }, "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Resources/deployments/deployment1", "name": "deployment1", "type": "Microsoft.Resources/deployments" }
--resource-group - Specifies the resource group for deployment
--template-file - Points to the ARM template file
Check the status and details of the Spot VM after deployment.
Terminal
az vm get-instance-view --resource-group myResourceGroup --name mySpotVM
Expected OutputExpected
{ "statuses": [ { "code": "PowerState/running", "displayStatus": "VM running", "level": "Info", "time": null } ] }
--resource-group - Specifies the resource group
--name - Specifies the VM name
Key Concept

If you remember nothing else from this pattern, remember: Spot VMs save money by using spare cloud capacity but can be stopped anytime when the cloud needs those resources back.

Common Mistakes
Not setting the VM priority to 'Spot' in the configuration.
The VM will be created as a regular VM and will not get the cost savings or eviction behavior of a Spot VM.
Always set the 'priority' property to 'Spot' in the VM configuration to enable Spot pricing.
Using Spot VMs for critical applications that cannot tolerate interruptions.
Spot VMs can be evicted at any time, causing downtime or data loss for critical workloads.
Use Spot VMs only for flexible, interruptible workloads like batch jobs or testing.
Not specifying an eviction policy, leading to unexpected VM deletion.
Without an eviction policy, the default might delete the VM instead of stopping it, causing loss of data and configuration.
Set the evictionPolicy to 'Deallocate' to stop the VM safely on eviction.
Summary
Create a resource group and network interface to prepare the environment.
Deploy a Spot VM using an ARM template with priority set to Spot and eviction policy to Deallocate.
Verify the VM status to ensure it is running and using Spot pricing.