Powershell — Convert PYUG VM to SPOT in Azure (UPDATE)

Aammir Mirza
6 min readAug 29, 2023

What is PYUG (Pay As You Go) Virtual Machine ?

Pay-as-you-go virtual machines (VMs) refer to a cloud computing model where users are charged based on their actual usage of virtual machine resources. In this model, customers can rent and utilize virtual machines from a cloud service provider on a flexible basis, and they are billed only for the computing resources they consume, such as CPU, memory, storage, and network bandwidth. For the time the resource was running.

Smart Plat Ops and SRE try to cut down the consumption cost with Start and Stop schedule of the NonProd VMs workloads, and can make some good savings. (will that save 80% ? Nah !!!)

What is SPOT VMs ?

Spot VMs provide users with access to spare capacity in Azure’s data centers at a significantly reduced cost compared to regular (on-demand) virtual machines. It’s similar in concept to “spot instances” offered by other cloud providers like Amazon Web Services (AWS).

What Microsoft says about SPOT ?
Use Spot Virtual Machines to buy unused compute capacity at significant cost savings. Deploy workloads that can handle interruptions and don’t need to be completed within a specific period of time. Run workloads for development, testing, quality assurance, advanced analytics, big data, machine learning and AI, batch jobs, rendering and trans-coding of videos, graphics, and images at a very low cost.

With Spot Virtual Machines, provision unused Azure compute capacity at deep discounts of up to 90 percent compared to pay-as-you-go prices.* Pay only up to the maximum price that you set in advance (optional). Spot VM instances are ideal for workloads that can be interrupted, providing scalability while reducing costs. Get special Azure pricing and benefits when running Windows Server workloads on spot VMs.

*Actual discounts may vary based on region, VM type, and Azure compute capacity available when the workload is deployed.

What kind of workloads can be changed to SPOT ?

On spot VMs, run only applications that can handle interruptions and don’t need to be completed within a specific time frame.

Spot VMs are ideal for the following types of workloads:

  • Dev/test environments, including continuous integration and continuous delivery (CI/CD) workloads.
  • Select high-performance computing scenarios, batch processing jobs, or visual rendering applications.
  • Big data, analytics, container-based, and large-scale stateless applications.
  • GitHub runners, Azure DevOps Agents
  • Member of AKS nodepool (upto 30%)
  • Member of VMSS

Refer the SPOT advisory link here to find appropriate VM for your workload

What If I don't want to DEALLOCATE / DELETE my VM because of cost changes ?

If you want to use Azure Spot Virtual Machines (VMs) but you’re concerned about the possibility of your Spot VMs being deallocated due to capacity constraints or price changes, there are a few strategies you can consider to mitigate this risk and make the most of Spot VMs while minimizing potential disruptions:

  • Availability Sets or Availability Zones
  • Use of Low-Priority VMs
  • Use Azure Spot Infrastructure as a Service (IaaS) VM Scale Sets
  • Custom Scripts and Checkpoints
  • Monitor Spot Instance Interruptions

You can set the maxPrice to -1 to indicate that the Azure Spot VM/VMSS should not be evicted for price reasons. Also, the default max price is -1 if it is not provided by you.

How to find the running cost of VM in your region based on instance family ?

What is the eviction percentage for the VMs in my region ?

Using Resource Graph Query, you can easily find out the best suitable SKU based on EvictionRate and SpotPrice in that region.

SpotResources
| where type =~ 'microsoft.compute/skuspotpricehistory/ostype/location'
| where sku.name matches regex @'(?i)Standard+_(?i)D|E|F|L[0-9]+_v[0-9]'
| where properties.osType in~ ('linux','windows') # OS Platform
| where location in~ ('westeurope', 'northeurope') #location to extract for
| project skuName = tostring(sku.name), osType = tostring(properties.osType), location, latestSpotPriceUSD = todouble(properties.spotPrices[0].priceUSD)
| join (SpotResources
| where type =~ 'microsoft.compute/skuspotevictionrate/location'
| where sku.name matches regex @'(?i)Standard+_(?i)D|E|F|L[0-9]+_v[0-9]'
| where location in~ ('westeurope', 'northeurope')
| project skuName = tostring(sku.name), location, spotEvictionRate = tostring(properties.evictionRate)) on $left.skuName == $right.skuName
| project-away skuName1, location1
| order by latestSpotPriceUSD asc

Results

Reference link

How to convert existing PYUG VM to SPOT ?

Powershell module to convert PYUG to SPOT VM

You can use the Powershell module ConvertTo-Spot to convert your existing PYUG VMs to SPOT. Usage and results shared down below.

PS> Install-Module -Name ConvertTo-SPOT

Example commands-

What-If (Running without -Force switch)

PS> ConvertTo-SPOT -subscriptionName 'aamirmirza-lab' -resourceGroup 'rg-spotvmss-1984' -vmName 'pyug2spot-vm' -verbose

From the above screen shot we can see that the PYUG VM exist and it is running with 2 extensions. Running the VM in What-If mode helps understand dependencies. Now we will run the same command with -force switch.

Force

PS> ConvertTo-SPOT -subscriptionName 'aamirmirza-lab' -resourceGroup 'rg-spotvmss-1984' -vmName 'pyug2spot-vm' -verbose -Force

With the -force switch we are initiating the conversion. From the below screen shot we can see that the conversion completed with backup/snapshots of the disks (both Data and OS), so that we have possibility to recover in-case of damage.

I strongly recommend to run it in sandbox or lab before running for any data critical workload (anyways we will never convert mission critical to SPOT — — LOL)

Does it really works ?

GIF for your reference :D

Success

Updated Content

I forgot to add the Limitations, which I am doing now. I am sorry if it caused issues to existing consumers of the module.

With latest and greatest updated version of the module 1.0.9.

I am handling few known issues.

  1. Detaching dependent resources — While creating a VM by default we have OSDisk Delete with VM option enabled. So I have added, detach mode changes for such VMs.

2. Checking for SecurityProfile/Security type of VM, If it is not set to Standard then conversion fails. Reason Link (Limitation). Which means only Standard Security profile VMs are allowed.

I don't know why I am hiding subscription ID in every screen shot, we are unknown friends and not threat to community, right ?

--

--

Aammir Mirza

Cloud Architect with 12 years of experience in managing cloud infrastructure and automation, integrating Azure cloud-based infra components