Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Bicep - Deploy a Subscription Budget using Azure CLI

·738 words·4 mins· 100 views · 5 likes ·
Azure CLI Azure Cloud Shell Budgets Microsoft

Managing your resources and services costs is crucial to your Azure subscriptions’ governance. That’s where Azure Budgets comes in. Azure Budgets helps us manage our cloud costs by setting spending limits. What if we could automate this process? That’s where Bicep and Azure CLI can help. This post shows you how to set up an Azure Budget using Bicep templates and Azure CLI.


Prerequisites #

Before you start, you’ll need the following to deploy and manage a budget resource with Bicep:

  • You need Azure CLI version 2.20.0 or later to deploy Bicep files on your local machine.
  • A text editor or IDE of your choice (Visual Studio Code with Bicep extension is my recommendation)
Create the Bicep file>

Create the Bicep file #

The first step in deploying a Bicep template is to create the Bicep file that defines your resources. Create a new file named budget.bicep. This file will contain the necessary code to define and configure your Azure Budget resource.

targetScope= 'subscription'

@description('Name of the project or solution')
param systemName string = 'demo'

@description('Environments to use')
param environment string

@description('Start date in the format yyyy-MM-dd')
param startDate string

@description('End date in the format yyyy-MM-dd')
param endDate string

@description('Maximum budget allowed')
param maximumBudget int

@description('List of contact emails for notifications')
param contactEmails array

var nameBudget = 'budget-sub-${systemName}-${environment}'

resource budget 'Microsoft.Consumption/budgets@2023-03-01' = {
  name: nameBudget
  properties: {
    timeGrain: 'Monthly'
    timePeriod: {
      startDate: startDate
      endDate: endDate
     category: 'Cost'
     amount: maximumBudget
     notifications: {
         enabled: true
         operator: 'GreaterThanOrEqualTo'
         threshold: 90
         thresholdType: 'Actual'
         contactEmails: contactEmails
        Forecast_Over_80_Percent: {
          operator: 'GreaterThanOrEqualTo'
          threshold: 80
          thresholdType: 'Forecasted'
          contactEmails: contactEmails     

output budgetName string =
output budgetId string =

The time frame a budget covers is known as timeGrain. Tracking of the budget amount will restart according to this period. The following values are supported: Annually, BillingAnnual, BillingMonth, BillingQuarter, Monthly, and Quarterly.

Deployment scope>

Deployment scope #

In Bicep, you can target your deployment to a resource group, subscription, management group, or tenant. In this case, when creating the Azure Budget resource, the valid deployment scopes are Azure Resource groups or Subscriptions. By default, when deploying a Bicep template, the scope where the resource should be deployed is a resource group. For this example, I will use a subscription as the scope.

Deploy the Azure Budget resource using Azure CLI>

Deploy the Azure Budget resource using Azure CLI #

Once your Bicep template is prepared, and you’ve selected your desired scope, you can proceed to deploy the template through the Azure CLI. To do so, execute the following commands.


Parameters #

Personalization is key to making your template reusable. With the parameters, you can easily tailor the template to your specific needs. You can use either inline parameters or a parameter file to pass parameter values. In my case, I will use a file to pass the parameters; here is an example.

  "$schema": "",
  "contentVersion": "",
  "parameters": {
    "systemName": {
      "value": "demo"
    "environment": {
      "value": "prod"
    "maximumBudget": {
      "value": 5000
    "contactEmails": {
      "value": ["[email protected]", "[email protected]"]
    "startDate": {
      "value": "2023-06-01"
    "endDate": {
      "value": "2024-06-01"

Important: Please note that the parameter file stores parameter values in plain text format. If you need to include a parameter with sensitive data, it’s recommended to store the value in a secure key vault.

Preview changes>

Preview changes #

Before deploying a Bicep file, you can preview the changes that will occur to your resources. Using what-if operations does not change existing resources; it simply shows you an output that includes color-coded results that allow you to see different changes.

az deployment sub what-if \
--template-file <filename>.bicep \
--parameters @<filename>.parameters.json \
--location <location>
Deploy the Budget resource>

Deploy the Budget resource #

Finally, to deploy the template, run the following command.

az deployment sub create \
--template-file <filename>.bicep \
--parameters @<filename>.parameters.json \
--location <location>
Validate the deployment>

Validate the deployment #

To verify that the budget resource was created correctly, you can either use the Azure Portal or the Azure CLI to check the created resources and their configurations. For Azure CLI, use the following command.

az consumption budget list \
--output table

References and useful links #

Thank you for taking the time to read my post. I sincerely hope that you find it helpful.