Skip to main content

Setting Up Azure Cost Management and Budgets

Create an Azure budget with 80% actual and 100% forecast alerts using the CLI, check spend, and cut waste with Advisor and auto-shutdown.

Overview and What You Will Learn

An Azure budget is a spending limit over a time period that sends an alert when actual or forecasted cost crosses a percentage of it. In this lab, you will check current spend, create a monthly budget with two alert thresholds using the CLI, estimate costs before deploying, and use Advisor and auto-shutdown to cut waste.

Why This Matters in Production

A budget alert turns a silent surprise into an early warning. Imagine a developer at a startup who spins up a test environment, forgets about it, and returns three weeks later to find the credit consumed by a VM nobody used after day one. An alert set on day one would have flagged the spend long before the money was gone.

Core Principles

Azure billing is consumption-based, so you pay for what you use, while Reserved Instances and Savings Plans exist separately as discounts for predictable workloads. A budget never stops spending on its own. It only notifies you, by email or by calling an action group, once cost crosses a threshold.

flowchart TD R[Resource created] --> M[Usage meter
records consumption] M --> C[Cost data
lags up to a day] C --> B{Budget threshold
crossed?} B -->|Yes| A([Alert email
or action group]) B -->|No| W[Keep watching] classDef step fill:#326CE5,color:#FFFFFF classDef decision fill:#F59E0B,color:#111827 classDef alert fill:#EF4444,color:#FFFFFF classDef ok fill:#10B981,color:#FFFFFF class R,M,C step class B decision class A alert class W ok

A budget alert can use two threshold types:

  1. Actual fires when real cost so far crosses the percentage.
  2. Forecasted fires when Azure predicts that your cost will cross the percentage by the end of the period, which gives earlier warning.
Remember

Cost data can lag real usage by up to a day, and budgets are evaluated against that data. An alert is a warning, not a hard stop, so a runaway resource can keep spending until someone acts.

Detailed Step-by-Step Practical Lab

How to Check Current Spend with the CLI

Register the cost providers once, then list recent usage. Columns vary by agreement type, and Cost analysis in the Portal is better for grouping by service or tag.

Bash
az provider register --namespace Microsoft.Consumption --wait
az provider register --namespace Microsoft.CostManagement --wait
az consumption usage list --top 10 --output table

How to Create a Budget with Alert Thresholds

Budgets are created through the Consumption REST API, which works the same from the CLI, an ARM template, or Terraform. You need Owner, Contributor, or Cost Management Contributor on the subscription. The amount uses your billing currency, and the start date must be the first day of a month.

Bash
SUB_ID=$(az account show --query id --output tsv)
cat > budget.json << 'BUDGETEOF'
{
"properties": {
"category": "Cost",
"amount": 2000,
"timeGrain": "Monthly",
"timePeriod": {
"startDate": "2026-10-01T00:00:00Z",
"endDate": "2027-10-01T00:00:00Z"
},
"notifications": {
"Actual_GreaterThan_80_Percent": {
"enabled": true,
"operator": "GreaterThan",
"threshold": 80,
"thresholdType": "Actual",
"contactEmails": ["rahul@devops.in"],
"locale": "en-us"
},
"Forecasted_GreaterThan_100_Percent": {
"enabled": true,
"operator": "GreaterThan",
"threshold": 100,
"thresholdType": "Forecasted",
"contactEmails": ["rahul@devops.in"],
"locale": "en-us"
}
}
}
}
BUDGETEOF
az rest --method put \
--url "https://management.azure.com/subscriptions/$SUB_ID/providers/Microsoft.Consumption/budgets/monthly-subscription-budget?api-version=2023-05-01" \
--body @budget.json
Tip

Set the first alert at 80% of the limit that matters to you, such as a trial credit or a departmental cap. An 80% alert leaves time to react, while a 100% alert often means the money is already spent.

How to Verify the Budget

Confirm the budget exists and see how much of it is already used.

Bash
az rest --method get \
--url "https://management.azure.com/subscriptions/$SUB_ID/providers/Microsoft.Consumption/budgets?api-version=2023-05-01" \
--query "value[].[name, properties.amount, properties.currentSpend.amount]" \
--output table

How to Estimate Cost Before Deploying

Use the Azure Pricing Calculator for a full estimate, or query the public Retail Prices API from Cloud Shell to compare prices for a specific size and region.

Bash
curl -sG "https://prices.azure.com/api/retail/prices" \
--data-urlencode "currencyCode=INR" \
--data-urlencode "\$filter=armRegionName eq 'centralindia' and armSkuName eq 'Standard_B2s' and priceType eq 'Consumption'" \
| jq -r '.Items[] | select(.productName | test("Windows") | not) | select(.meterName | test("Spot|Low Priority") | not) | [.productName, .meterName, .retailPrice, .currencyCode] | @tsv'

Change armRegionName to compare regions, because the same size can cost meaningfully more or less depending on where it runs.

How to Find Savings with Azure Advisor

Advisor needs several days of usage history before it can flag an idle or oversized resource, so a VM created minutes ago will not appear yet.

Bash
az advisor recommendation list --category Cost --output table

If a recommendation suggests downsizing an underused resource, review it against your real usage pattern before applying it.

How to Set Auto-Shutdown on a Dev or Test VM

A dev VM that runs 24/7 but is used only during business hours wastes about two-thirds of its runtime. Auto-shutdown deallocates the VM at a fixed time every day, and the time is in UTC, so 1630 UTC is 22:00 in India.

Bash
az vm auto-shutdown \
--resource-group rg-dev-mumbai \
--name vm-dev-mumbai-1 \
--time 1630

Deallocation stops compute charges, but the VM's disks and any static public IP continue to bill.

How to Clean Up

Delete the lab budget so it does not keep alerting on a subscription you reuse.

Bash
az rest --method delete \
--url "https://management.azure.com/subscriptions/$SUB_ID/providers/Microsoft.Consumption/budgets/monthly-subscription-budget?api-version=2023-05-01"

Production Best Practices & Common Pitfalls

Common Mistake

Creating a free trial or pay-as-you-go account and deploying test resources without ever setting a budget alert. One oversized VM or an unexpectedly busy test workload can use far more than planned before anyone notices, because nothing was watching.

Tip

Tag every resource with owner and environment, such as env=dev and owner=rahul. Cost analysis can then group spend by tag, which shows who is responsible for a spike.

Security

Contact emails in a budget receive spend data. Send them to a shared team mailbox or an action group rather than one person's inbox, so alerts are not lost when someone leaves.

  • Use an action group for automatic responses. Budgets can call an action group, which can trigger an Automation runbook or a Logic App to stop non-production VMs when a threshold is crossed.
  • Scope budgets to match ownership. Create separate budgets at subscription, resource group, or tag level so each team sees its own spend.
  • Combine Actual and Forecasted alerts. A forecast alert warns you mid-month that the trend will overshoot, while an actual alert confirms the money has been spent.
  • Check region pricing before choosing one. The same VM size varies in price by region, so compare using your real target region.
  • Review Advisor on a schedule. New recommendations appear as usage history grows, so a monthly check catches idle resources early.

Quick Reference & Troubleshooting Commands

Command Description
az consumption usage list --top 10 List recent usage and cost line items
az rest --method put --url .../budgets/NAME Create or update a budget
az rest --method get --url .../budgets List budgets and current spend
az advisor recommendation list --category Cost List cost-saving recommendations
az vm auto-shutdown --time HHMM Schedule a daily shutdown in UTC
az provider register --namespace Microsoft.Consumption Fix missing cost data from an unregistered provider

Explore More in Azure Fundamentals and Governance

All 6 Topics

Frequently Asked Questions

Is Setting Up Azure Cost Management and Budgets 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 Setting Up Azure Cost Management and Budgets topic cover?

Create an Azure budget with 80% actual and 100% forecast alerts using the CLI, check spend, and cut waste with Advisor and auto-shutdown.

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