Skip to main content

Task 04: Create Cost Budgets

Runbook Azure

DOCUMENT CATEGORY: Runbook
SCOPE: Cost Management budgets of the landing zone
PURPOSE: Get an e-mail before the Azure cost of the instance passes a set amount
MASTER REFERENCE: Microsoft Learn — Tutorial: Create and manage budgets

Status: Active

Overview​

A Cost Management budget tracks the Azure cost of a subscription or of one resource group over a period (a month, a quarter or a year) and sends an e-mail when the cost passes a percentage of the amount. The Azure side of an Azure Local instance can cost money (the Azure Local service fee per physical core unless Azure Hybrid Benefit waives it, the Arc-enabled services you turn on, Azure Monitor and Log Analytics ingestion, Key Vault, storage for the cloud witness), so a budget is a free early warning.

Free, and it never stops anything

Budgets are part of Cost Management and cost nothing. A budget does not cap the spending or stop, limit or delete any resource by itself; the budgets of this task send e-mails only. A budget can also call an Azure Monitor action group whose automation acts on resources, which this task does not set up (Microsoft Learn: Tutorial: Create and manage budgets).

What This Accomplishes​

  • Actual cost alerts — an e-mail when the cost of the period passes each threshold (80% and 100% of the amount unless you set others)
  • Forecast alerts — optional: an e-mail when the forecast cost of the period is going to pass a threshold, before it happens
  • Per subscription or per resource group — one budget for the whole subscription, or one for the resource group of the instance
  • Kept as written — run the task again after a change in infrastructure.yml: the budget is updated to the file

Prerequisites​

PrerequisiteDetail
SubscriptionsThe subscriptions exist (Task 02); for a resource group budget, the resource group exists (Task 03)
PermissionsCost Management Contributor, Contributor or Owner on each subscription or resource group that gets a budget (Microsoft Learn: Assign access to Cost Management data)
Authenticated Azure sessionSee Authentication
infrastructure.ymllanding_zone.budgets filled in (schema 5.6.30)

Budget Plan​

The example below uses Improbability Cloud (IIC). Replace names and amounts with values from your infrastructure.yml. The amount is in the billing currency of the subscription.

BudgetScopeAmountPeriodAlerts
budget-iic-azl-monthlySubscription sub-iic-azure-local1000Monthly80% and 100% of the actual cost, 100% of the forecast cost
budget-iic-mgmt-quarterlyResource group rg-iic-mgmt-01 of sub-iic-management600Quarterly90% of the actual cost
landing_zone:
budgets:
- name: "budget-iic-azl-monthly"
subscription: "sub-iic-azure-local" # a name in landing_zone.subscriptions; empty: azure.subscription_id
amount: 1000
time_grain: Monthly # Monthly | Quarterly | Annually
thresholds: [80, 100] # percent of the amount, actual cost (default 80 and 100)
forecast_thresholds: [100] # percent of the amount, forecast cost (optional)
contact_emails: ["cloud-costs@example.com"]
- name: "budget-iic-mgmt-quarterly"
scope: resource_group
subscription: "sub-iic-management"
resource_group: "rg-iic-mgmt-01"
amount: 600
time_grain: Quarterly
thresholds: [90]
contact_emails: ["cloud-costs@example.com"]

Values from infrastructure.yml​

The scripts read these keys from infrastructure.yml (schema 5.6.30). Each key, with its type, default and example, is in the variables reference.

azure.tenant_id, azure.subscription_id
landing_zone.subscriptions[].name, .id the subscription of each entry
landing_zone.budgets[].name 1 to 63 letters, digits, hyphens and underscores; unique in its scope (required)
landing_zone.budgets[].scope subscription (default) or resource_group
landing_zone.budgets[].subscription a name in landing_zone.subscriptions; empty: azure.subscription_id
landing_zone.budgets[].resource_group the resource group, when scope is resource_group
landing_zone.budgets[].amount greater than 0, in the billing currency (required)
landing_zone.budgets[].time_grain Monthly (default), Quarterly or Annually (the Web Direct only grains BillingMonth, BillingQuarter and BillingAnnual are not offered by the toolkit)
landing_zone.budgets[].start_date YYYY-MM-01; empty: the first day of the current month
landing_zone.budgets[].end_date YYYY-MM-DD; empty: ten years after the start (set by Azure)
landing_zone.budgets[].thresholds percentages of the actual cost (default 80 and 100)
landing_zone.budgets[].forecast_thresholds percentages of the forecast cost (optional)
landing_zone.budgets[].contact_emails at least one address (required)

Rules from Microsoft Learn (Budgets - Create Or Update): the start date is the first of a month, on or after 2017-06-01 and at most twelve months ahead; a budget has at most five notifications, so thresholds and forecast_thresholds together list at most five percentages; each percentage is greater than 0 and at most 1000. The scripts check these rules before they call Azure and stop with exit code 2 when a value breaks one.


Execution Options​

Azure Portal​

When to use: One budget, or to look at what the scripts created

Procedure​

  1. In the Azure portal, open Cost Management + Billing and select Cost Management, then Budgets.
  2. Select the scope: the subscription (for example sub-iic-azure-local) or the resource group (for example rg-iic-mgmt-01).
  3. Select + Add.
  4. Create a budget:
  • Name: the name of the entry
  • Reset period: Monthly, Quarterly or Annually (time_grain)
  • Creation date and Expiration date: start_date and end_date
  • Amount: amount
  1. Set alerts:
  • One line per percentage of thresholds with type Actual, and one per percentage of forecast_thresholds with type Forecasted
  • Alert recipients (email): the contact_emails
  1. Select Create.

Validation​

  • Each budget of the file is listed under Budgets on its scope
  • Amount, reset period and alert percentages match infrastructure.yml
  • The alert recipients are the addresses of contact_emails

Troubleshooting​

SymptomErrorResolution
Permission deniedAuthorizationFailed or RBACAccessDenied (HTTP 403)Give the account Cost Management Contributor, Contributor or Owner on the subscription or resource group. Contributor and Cost Management Contributor can change every setting of their own budgets but only the amount of a budget someone else created; Owner can change any budget (Microsoft Learn: Tutorial: Create and manage budgets)
Start date refusedHTTP 400 that names the start dateThe start date is the first of a month, not more than twelve months ahead; a past start date must fall in the current period of the time grain. Leave start_date out to use the first day of the current month
New or unsupported subscriptionSubscriptionNotFound or SubscriptionTypeNotSupportedA new subscription can take up to 48 hours before Cost Management features work; not every offer is supported (Microsoft Learn: Troubleshoot common Cost Management errors)
No e-mail arrives—The alert fires when the cost passes the threshold; alerts are not real time: cost data usually arrives within 8 to 24 hours (sometimes later) and budgets are evaluated once a day, so an alert can come after the cost passed the threshold. Check the recipients of the budget in the portal
Period cannot changeBudget time grain cannot be updatedThe scripts stop before the write and say so. Delete the budget in the portal (Cost Management, Budgets) or give the entry another name
Exit code 2A message names the keyFix the value in infrastructure.yml; nothing was sent to Azure

Next Steps​

References​


Alternatives​

The procedures in this task use the methods shown in the tabs above. The toolkit is PowerShell 7 only and has no Bash scripts. A few tasks that act on Azure also have an Azure CLI script written in PowerShell. The script status page of the toolkit lists which task has which script.

PreviousUpNext
Task 03 — Create Resource GroupsFull Deployment OverviewPhase 02 — Resource Providers

Version Control​

VersionDateAuthorChanges
1.0.02026-11-23Hybrid Cloud SolutionsInitial release: Deploy-CostBudgets.ps1 and Deploy-CostBudgets.azcli.ps1 (owner directive D128)