Using Developer Tools to get the Payload to Create Azure Budget Alerts for Action Groups (New-AzConsumptionBudget)

Are you trying to create a budget with PowerShell using New-AzConsumptionBudget on a resource group? You might have tried the code reference on this page New-AzConsumptionBudget (Az.Billing) | Microsoft Learn.

# If you update the sample command and try to execute
New-AzConsumptionBudget -ResourceGroupName budgetThis -Amount 60 -Name PSBudgetRG -Category Cost -StartDate 2022-11-01 -EndDate 2024-11-01 -TimeGrain Monthly

If you’re not using a subscription in an EA, you’ll get this error New-AzConsumptionBudget: Operation returned an invalid status code 'BadRequest'

Running get-error doesn’t provide much in the way of help to resolve. After further digging you’ll find the PowerShell reference page, you’ll probably find that Microsoft docs states you have to be an “EA customer, you can create and edit budgets programmatically using the Azure PowerShell module. However, we recommend that you use REST APIs to create and edit budgets because CLI commands might not support the latest version of the APIs."

If you are using EA subscription it still doesn’t create the alerts and the PowerShell doco doesn’t seem easy to use to create these notifications either.

Hopefully, you found this script SetConsumptionBudget.ps1 Create a Consumption Budget for ResourceGroups in Azure (github.com). It is a complete script and is helpful. This is a good implementation of the content documented here. Budgets - Create Or Update - REST API (Azure Consumption) | Microsoft Learn.

What if you couldn’t find a pre-created script what would you do? Or if you wanted a simplified call rather than the long doc example, in this case, I wanted to leverage using an Action Group.

Let’s create a budget through the Azure portal called “CreatedWithUI” and use the browser developer tools (F12) to capture the traffic requests to the Azure API.

After you click “Create” you’ll see traffic and see the request payload

You can copy the properties section, you just need to add the properties name back in.

With some changes to the syntax object, we can create a script to create the budget alerting an Action Group.

# Converted Syntax for use in script
$bodyObject = @{
    "properties"= @{
        "timePeriod" = @{
            "startDate"= "2022-10-01T00:00:00Z"
            "endDate"= "2024-09-30T00:00:00Z"
        }
        "timeGrain" = "Monthly"
        "amount"= 62
        "category"= "Cost"
        "notifications" =@{
            "forecasted_GreaterThan_90_Percent"= @{
                "enabled" = $true
                "operator"= "GreaterThan"
                "threshold"= 90
                "contactGroups"= @("/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/cloudmodernization-rg/providers/microsoft.insights/actionGroups/BudgetAlerts")
                "thresholdType"= "Forecasted"
            }
        }
    }
}

The last piece of data we need is the Action Group, which still works in PowerShell

# Parameters
$ActionGroupRGName = "cloudmodernization-rg"
$ActionGroupEmails = @("admin@yourorg.com")
$ActionGroupName = "BudgetAlerts"

# Get existing action Group ID
$ActionGroupId = (Get-AzActionGroup -resourcegroup  cloudmodernization-rg  -Name BudgetAlerts).id

# Create New Action Group
$AlertEmail = New-AzActionGroupReceiver -EmailAddress $ActionGroupEmail -Name ForcastBudgetAlert
$ActionGroupId = (Set-AzActionGroup -ResourceGroupName ($cb.ResourceGroup) -Name $ActionGroupName -ShortName $ActionGroupName -Receiver $AlertEmail).Id

Finally, if you pull all this together calling Invoke-RestMethod it would like something like

# Parameters
$subscriptionId = "00000000-1111-2222-3333-444444444444"
$resourceGroupName = "cloudmodernization-rg"
$ActionGroupName = "BudgetAlerts"
$budgetName = "$resourceGroupName-cb"

# Get existing action Group ID
$ActionGroupId = (Get-AzActionGroup -resourcegroup  $resourceGroupName -Name $ActionGroupName ).id

$bodyObject = @{
    "properties"= @{
        "timePeriod" = @{
            "startDate"= "2022-10-01T00:00:00Z"
            "endDate"= "2024-09-30T00:00:00Z"
        }
        "timeGrain" = "Monthly"
        "amount"= 62
        "category"= "Cost"
        "notifications" = @{
            "forecasted_GreaterThan_90_Percent"= @{
                "enabled" = $true
                "operator"= "GreaterThan"
                "threshold"= 90
                "contactGroups"= @($ActionGroupId)
                "thresholdType"= "Forecasted"
            }
        }
    }
    "filter" =@{}
}

$bearerToken=(Get-AzAccessToken).token
$headers = @{'Authorization' = "Bearer $bearerToken"}
$RequestBody = $bodyObject | convertto-json -Depth 15

$URI = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Consumption/budgets/$budgetName"+"?api-version=2021-10-01"
Invoke-RestMethod -Method Put -ContentType  "application/json; charset=utf-8" -Uri $URI -Headers $headers  -Body $RequestBody

Here are the three budgets $60, $61, and $62 created through this article.

This was aimed at showing you how to get to the payload submitted to the Azure Portal to solve a problem using PowerShell and how to revert to using a direct Azure API call.