0
0
Azurecloud~5 mins

Azure Spot VMs for cost savings - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes cloud servers cost a lot. Azure Spot VMs let you use spare servers at a lower price. They can be taken back by Azure anytime, so they are good for tasks that can pause and restart.
When running batch jobs that can stop and start without problems.
When testing software that does not need to run all the time.
When you want to save money on virtual machines for flexible workloads.
When running background tasks that can be interrupted and resumed later.
When you want to try cloud servers at a lower cost before buying full-price ones.
Config File - azuredeploy.json
azuredeploy.json
{
  "$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.

Commands
Create a resource group to hold all related Azure resources in the East US region.
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 - Sets the name of the resource group
--location - Sets the Azure region for the resource group
Deploy the ARM template to create the Spot VM with the given admin password.
Terminal
az deployment group create --resource-group myResourceGroup --template-file azuredeploy.json --parameters adminPassword=MyStrongP@ssw0rd
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Resources/deployments/azuredeploy", "name": "azuredeploy", "properties": { "provisioningState": "Succeeded", "outputs": {} } }
--resource-group - Specifies the resource group to deploy into
--template-file - Points to the ARM template file
--parameters - Passes parameters like admin password securely
Check the current status of the Spot VM to confirm it is running.
Terminal
az vm get-instance-view --resource-group myResourceGroup --name spot-vm-example --query instanceView.statuses[1]
Expected OutputExpected
{ "code": "PowerState/running", "displayStatus": "VM running", "level": "Info", "time": null }
--resource-group - Specifies the resource group of the VM
--name - Specifies the VM name
--query - Filters output to show VM power state
Key Concept

If 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.

Common Mistakes
Trying to use Spot VMs for critical apps that must run all the time.
Spot VMs can be evicted by Azure at any time, causing downtime.
Use Spot VMs only for workloads that can handle interruptions and restart later.
Not setting evictionPolicy or maxPrice in the VM configuration.
Without evictionPolicy, the VM may be deleted instead of deallocated, losing data. Without maxPrice, costs may be unpredictable.
Set evictionPolicy to 'Deallocate' and maxPrice to -1 or a specific value to control eviction and cost.
Deploying Spot VMs in regions or sizes with no available spare capacity.
The deployment will fail or the VM will not start because no Spot capacity is available.
Check Azure Spot VM availability for your region and VM size before deploying.
Summary
Create a resource group to organize Azure resources.
Deploy an ARM template that defines a Spot VM with eviction policy and max price settings.
Check the VM status to confirm it is running and ready to use.