Skip to main content

Reducing Azure VM Costs with Spot Instances and Auto-Shutdown

Learn to cut real VM spend using auto-shutdown for dev VMs and Spot Instances for fault-tolerant batch workloads.

Overview and What You Will Learn

In this lab, you will configure auto-shutdown on a development VM so it stops billing outside business hours, then create a Spot VM and observe its discounted pricing and eviction behavior - two genuinely different cost-reduction techniques for two genuinely different situations.

Why This Matters in Production

A forgotten test VM at a startup runs 24/7 for months because nobody remembered to turn it off after business hours, quietly costing as much as a small production workload for a resource that was only ever used a few hours a day. Auto-shutdown alone can cut that VM's compute cost by more than half, simply by not billing for the two-thirds of the day nobody was using it.

Core Principles

Auto-shutdown and Spot Instances solve two different cost problems and should not be confused with each other.

◈ DIAGRAM
+------------------------------------------+
| Auto-Shutdown |
| For: dev/test VMs used only during |
| business hours |
| Effect: VM powers off on a schedule, |
| stops billing while off |
+------------------------------------------+
| Spot Instances |
| For: fault-tolerant batch or stateless |
| workloads that can handle interruption |
| Effect: steep discount, but Azure can |
| reclaim the VM with short notice |
+------------------------------------------+

Auto-shutdown is appropriate for any VM with a predictable, non-24/7 usage pattern. Spot Instances are appropriate specifically for workloads that can tolerate being interrupted and restarted elsewhere - never for anything customer-facing that needs to be reliably available.

Detailed Step-by-Step Practical Lab

  1. Create a Resource Group and a development VM:
Bash
az group create --name rg-cost-lab-mumbai --location centralindia
az vm create \
--resource-group rg-cost-lab-mumbai \
--name vm-dev-lab \
--image Ubuntu2204 \
--size Standard_B1s \
--admin-username azureadmin \
--generate-ssh-keys
  1. Configure auto-shutdown on the dev VM to power off automatically every night at 9 PM:
Bash
az vm auto-shutdown \
--resource-group rg-cost-lab-mumbai \
--name vm-dev-lab \
--time 2100
  1. Confirm the auto-shutdown schedule was applied:
Bash
az vm show \
--resource-group rg-cost-lab-mumbai \
--name vm-dev-lab \
--query "additionalCapabilities" --output json
  1. Create a separate Spot Instance for a batch-processing workload, accepting Azure's spare capacity at a steep discount:
Bash
az vm create \
--resource-group rg-cost-lab-mumbai \
--name vm-batch-spot \
--image Ubuntu2204 \
--size Standard_B2s \
--priority Spot \
--eviction-policy Deallocate \
--max-price -1 \
--admin-username azureadmin \
--generate-ssh-keys
Note

--max-price -1 means you're willing to pay up to the current on-demand price rather than setting a hard cap - this is the simplest configuration to start with. --eviction-policy Deallocate means an evicted Spot VM is stopped and deallocated rather than deleted, so its disk and configuration are preserved for when it can be restarted.

  1. Confirm the Spot VM was created with the expected pricing model:
Bash
az vm show \
--resource-group rg-cost-lab-mumbai \
--name vm-batch-spot \
--query "priority" --output tsv
## Expected: "Spot"
  1. Review Azure Advisor's cost recommendations to see if any additional rightsizing opportunities exist on VMs already running:
Bash
az advisor recommendation list \
--category Cost \
--output table
  1. Clean up:
Bash
az group delete --name rg-cost-lab-mumbai --yes --no-wait

Production Best Practices & Common Pitfalls

Common Mistake

Using a Spot Instance for a customer-facing or otherwise critical workload to save money, then experiencing an unexpected outage when Azure reclaims the capacity with only a couple of minutes' notice. Spot pricing is a genuine discount, but it comes with a real availability trade-off that only fits fault-tolerant, interruption-tolerant workloads.

Tip

Combine auto-shutdown with a corresponding auto-start schedule (via Azure Automation or a Logic App) for dev/test environments used on a predictable daily pattern, so the environment is ready and running again automatically each morning without anyone needing to start it manually.

  • Auto-shutdown only helps VMs with genuinely predictable idle time. A production VM serving live traffic around the clock gets no benefit from auto-shutdown, since it's never actually idle in the first place.
  • Reserved Instances are the right tool for stable, predictable, always-on production workloads - a separate cost-reduction lever from both auto-shutdown and Spot, appropriate when you know a VM size will run continuously for the next 1-3 years.

Quick Reference & Troubleshooting Commands

Command Description
az vm auto-shutdown Configure a scheduled shutdown time for a VM
az vm create --priority Spot Create a Spot Instance at a discounted price
az advisor recommendation list --category Cost List cost-saving recommendations
az vm show --query "priority" Confirm whether a VM is Spot or regular pricing

Explore More in Azure Compute Services

All 6 Topics

Frequently Asked Questions

Is Reducing Azure VM Costs with Spot Instances and Auto-Shutdown free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Reducing Azure VM Costs with Spot Instances and Auto-Shutdown topic cover?

Learn to cut real VM spend using auto-shutdown for dev VMs and Spot Instances for fault-tolerant batch workloads.