Skip to main content

Reducing Azure VM Costs with Spot and Shutdown

Cut Azure VM spend with auto-shutdown for dev and test VMs and Spot VMs for interruption-tolerant batch jobs, and see where each one fits.

Overview and What You Will Learn

Auto-shutdown powers a VM off on a daily schedule so it stops billing for compute while nobody uses it, and an Azure Spot VM runs on spare capacity at a deep discount in exchange for the risk that Azure takes the capacity back. They solve different problems: one removes idle hours, the other lowers the hourly price of work that can be interrupted. In this lab, you will schedule auto-shutdown for a dev VM, create a Spot VM, simulate an eviction, and see how each technique fits a workload.

  • Scheduling auto-shutdown correctly, including the time zone
  • Creating a Spot VM and choosing an eviction policy and max price
  • Simulating an eviction and restarting the VM
  • Choosing between auto-shutdown, Spot, and commitment discounts
  • Reading Azure Advisor cost recommendations

Why This Matters in Production

A forgotten test VM at a startup runs around the clock for months, though it is only used for a few hours each working day. It quietly costs as much as a small production workload. A nightly shutdown removes most of the idle hours, and for a batch job that can be retried, a Spot VM cuts the price of the hours that remain. Neither change touches customer-facing systems.

Core Principles

Pick the technique from the shape of the workload. Auto-shutdown fits VMs with predictable idle time, Spot fits work that tolerates interruption, and a VM that must run continuously and reliably is a candidate for a reservation or savings plan instead.

flowchart TD W([VM workload]) --> Q1{Used only during
working hours?} Q1 -->|Yes| AS[Auto-shutdown
plus auto-start] Q1 -->|No| Q2{Tolerates
interruption?} Q2 -->|Yes| SP[Spot VM
deep discount] Q2 -->|No| RI[Reservation or
savings plan] classDef start fill:#D1FAE5,color:#1E293B classDef dec fill:#F97316,color:#FFFFFF classDef opt fill:#326CE5,color:#FFFFFF class W start class Q1,Q2 dec class AS,SP,RI opt
Technique Best for Trade-off
Auto-shutdown Dev and test VMs with idle nights VM is unavailable while off
Spot VM Batch, CI and stateless workers Can be evicted at short notice
Reservation or savings plan Steady always-on production One or three year commitment

When Azure needs the capacity back, or the Spot price rises above your maximum, it evicts the VM and sends a notice about 30 seconds ahead through the Scheduled Events service. A Deallocate eviction policy stops the VM and keeps its disks so you can start it again, and a Delete policy removes the VM and its disks.

Compute billing stops only when a VM is deallocated. Auto-shutdown deallocates the VM, but its disks, and any static public IP address, continue to be billed.

Detailed Step-by-Step Practical Lab

Set the variables used throughout the lab. The Spot VM uses a D-series size because B-series sizes do not support Spot.

Bash
export RG=rg-cost-lab-india
export LOCATION=centralindia
  1. Create the resource group and a small development VM:
Bash
az group create --name $RG --location $LOCATION
az vm create \
--resource-group $RG \
--name vm-dev-lab \
--image Ubuntu2204 \
--size Standard_B1s \
--admin-username azureadmin \
--generate-ssh-keys
  1. Schedule auto-shutdown. The --time value is in UTC, so 15:30 UTC is 9 PM in India:
Bash
az vm auto-shutdown \
--resource-group $RG \
--name vm-dev-lab \
--time 1530
Remember

Auto-shutdown times are UTC. Entering 2100 shuts the VM down at 2:30 AM India time, so always convert from your local time zone first.

  1. Confirm the schedule exists. Auto-shutdown is stored as a separate schedule resource named after the VM:
Bash
az resource show \
--resource-group $RG \
--resource-type Microsoft.DevTestLab/schedules \
--name shutdown-computevm-vm-dev-lab \
--query "{status:properties.status, timeUtc:properties.dailyRecurrence.time, zone:properties.timeZoneId}" \
--output table
  1. Create a Spot VM for a batch workload. A max price of -1 means you will pay up to the regular price, so the VM is evicted only when Azure needs the capacity:
Bash
az vm create \
--resource-group $RG \
--name vm-batch-spot \
--image Ubuntu2204 \
--size Standard_D2s_v5 \
--priority Spot \
--eviction-policy Deallocate \
--max-price -1 \
--admin-username azureadmin \
--generate-ssh-keys

If the create fails with a capacity error, try another size or region, because Spot capacity varies.

  1. Confirm the pricing model, eviction policy and max price:
Bash
az vm show \
--resource-group $RG \
--name vm-batch-spot \
--query "{priority:priority, eviction:evictionPolicy, maxPrice:billingProfile.maxPrice}" \
--output table
  1. Simulate an eviction so you can see what a real one looks like. The VM receives the notice and is then deallocated, which can take a minute or two:
Bash
az vm simulate-eviction --resource-group $RG --name vm-batch-spot
az vm get-instance-view \
--resource-group $RG \
--name vm-batch-spot \
--query "instanceView.statuses[?starts_with(code,'PowerState')].displayStatus | [0]" \
--output tsv

Repeat the second command until it reports VM deallocated. This is exactly what your workload must survive: stop cleanly, then resume when the VM starts again.

  1. Start the evicted VM again, which is possible because the policy was Deallocate:
Bash
az vm start --resource-group $RG --name vm-batch-spot
  1. Review Azure Advisor's cost recommendations. A brand new VM has no usage history, so expect results only for older VMs, and Advisor needs several days of metrics:
Bash
az advisor recommendation list --category Cost --output table
  1. Clean up:
Bash
az group delete --name $RG --yes --no-wait

Production Best Practices & Common Pitfalls

Common Mistake

Running a customer-facing or stateful workload on a Spot VM to save money, then suffering an outage when Azure reclaims the capacity with about 30 seconds of warning. Spot only fits work that can be stopped, retried and resumed.

Tip

Pair auto-shutdown with a morning auto-start for dev and test environments, using Azure Automation, a Logic App or a scheduled pipeline, so the environment is running again before the team arrives.

  • Check the Spot price history and eviction rate before you choose a size. The portal shows both per size and region, and a size with a low eviction rate is a much safer batch target.
  • Make Spot workloads restartable. Checkpoint progress, keep state off the VM, and listen for the scheduled eviction event to drain work in the final seconds.
  • Use scale sets for Spot at scale. A Virtual Machine Scale Set with Spot priority, or a mix of Spot and regular instances, replaces evicted capacity automatically.
  • Remember what still bills. A deallocated VM still pays for its disks, and a static public IP, so delete unused VMs entirely instead of leaving them stopped for months.
  • Auto-shutdown helps only when there is real idle time. A VM serving traffic around the clock is never idle, so use a reservation or savings plan for steady production instead.
  • Rightsize first. Check Advisor and metrics before buying commitments, because a discount on an oversized VM is still a waste.

Quick Reference & Troubleshooting Commands

Command Description
az vm auto-shutdown --time HHMM Schedule a daily shutdown, in UTC
az vm auto-shutdown --off Remove the shutdown schedule
az vm create --priority Spot --eviction-policy Deallocate Create a Spot VM that is stopped, not deleted, on eviction
az vm simulate-eviction Trigger a test eviction on a Spot VM
az vm get-instance-view --query instanceView.statuses Check a VM's power state
az advisor recommendation list --category Cost List cost recommendations
Problem Cause Fix
Spot VM creation fails on size B-series sizes do not support Spot Use a D, E or F series size
SkuNotAvailable or capacity error No spare Spot capacity in that size and region Try another size, zone or region
VM shuts down at the wrong hour Time was entered in local time Re-run with the UTC equivalent
Costs continue after shutdown Disks and static IPs still bill Delete resources that are no longer needed
Evicted VM will not restart Not enough capacity at that moment Retry later, or deploy in another size or region

Explore More in Azure Compute Services

All 6 Topics

Frequently Asked Questions

Is Reducing Azure VM Costs with Spot and 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 and Shutdown topic cover?

Cut Azure VM spend with auto-shutdown for dev and test VMs and Spot VMs for interruption-tolerant batch jobs, and see where each one fits.

Found a mistake, an outdated step or a broken link?