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.
+------------------------------------------+| 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
- Create a Resource Group and a development VM:
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- Configure auto-shutdown on the dev VM to power off automatically every night at 9 PM:
az vm auto-shutdown \ --resource-group rg-cost-lab-mumbai \ --name vm-dev-lab \ --time 2100- Confirm the auto-shutdown schedule was applied:
az vm show \ --resource-group rg-cost-lab-mumbai \ --name vm-dev-lab \ --query "additionalCapabilities" --output json- Create a separate Spot Instance for a batch-processing workload, accepting Azure's spare capacity at a steep discount:
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-keysNote
--max-price -1means 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 Deallocatemeans an evicted Spot VM is stopped and deallocated rather than deleted, so its disk and configuration are preserved for when it can be restarted.
- Confirm the Spot VM was created with the expected pricing model:
az vm show \ --resource-group rg-cost-lab-mumbai \ --name vm-batch-spot \ --query "priority" --output tsv## Expected: "Spot"- Review Azure Advisor's cost recommendations to see if any additional rightsizing opportunities exist on VMs already running:
az advisor recommendation list \ --category Cost \ --output table- Clean up:
az group delete --name rg-cost-lab-mumbai --yes --no-waitProduction Best Practices & Common Pitfalls
Common MistakeUsing 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.
TipCombine 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 |