azureazure-rmazure-cli2

Find cheapest spot supported size / sku via az CLI or terraform provider


Trying to automate spot instance creation using Azure CLI. Neither az vm list-sizes nor az vm list-skus --resource-type virtualMachines seems to show any "spot supported" filter(s).

I looked at terraform azurerm provider, but did not find any data source related to this under compute.

How does one figure out cheapest spot instance from a given region that meets some criteria (core count, memory, etc) ?

C:\Users\foo> az --version
azure-cli                         2.38.0

core                              2.38.0
telemetry                          1.0.6

Dependencies:
msal                            1.18.0b1
azure-mgmt-resource             21.1.0b1

Python location 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe'
Extensions directory 'C:\Users\foo\.azure\cliextensions'

Python (Windows) 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 15:58:59) [MSC v.1929 32 bit (Intel)]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.

Please let us know how we are doing: https://aka.ms/azureclihats
and let us know if you're interested in trying out our newest features: https://aka.ms/CLIUXstudy

C:\Users\foo> terraform --version
Terraform v1.2.6
on windows_amd64


Solution

  • Looking at the documentation:

    The following VM sizes are not supported for Azure Spot Virtual Machines:

    • B-series
    • Promo versions of any size (like Dv2, NV, NC, H promo sizes)

    So you could write an az cli query to exclude them:

    az vm list-sizes --location "<location>" `
      --query "[?!contains(name, 'Promo') || !contains(name, 'Standard_B')]"
    

    The Azure Pricing API could then help to find out pricing:

    az rest --method get `
      --uri "https://prices.azure.com/api/retail/prices?$filter=serviceName eq 'Virtual Machines' and armRegionName eq '<location>' and (contains(armSkuName, 'Promo') eq false and contains(armSkuName, 'Standard_B') eq false)"