Skip to content

Instantly share code, notes, and snippets.

@TiloGit
Created May 8, 2025 22:23
Show Gist options
  • Save TiloGit/d20610a36e0ecbe43aa6b1570561535b to your computer and use it in GitHub Desktop.
Save TiloGit/d20610a36e0ecbe43aa6b1570561535b to your computer and use it in GitHub Desktop.
AZ Create budget alert for few subscriptions.
subs=(
52zzzzzzzzzzzzzzzzzzzzzzzzzzzzz9b6dd
a2zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz480d
1azzzzzzzzzzzzzzzzzzzzzzzzzzzzzz9a1e
)
##now run
for sub in ${subs[@]}
do
echo "$(date -Is) Now work on $sub"
##create budget via CLI
#sub='a2czzzzzzzzzzzzzzzzzzzzzzzz80d'
emailToNotify='[email protected]'
amount=210
budgetName_suffix='_BudgetAlert'
azapiversion='2024-08-01'
enddatePlusYears=15
##calc some of the values
enddate=$(date -d "`date +%Y%m01` +$enddatePlusYears years" +%Y-%m-%d)
startdate=$(date -d "`date +%Y%m01` " +%Y-%m-%d)
SubName=$(az account subscription show --id $sub --query "[displayName"] -o tsv)
budgetName=$SubName$budgetName_suffix
### Invoke request
az rest --method PUT --url "https://management.azure.com/subscriptions/$sub/providers/Microsoft.Consumption/budgets/$budgetName?api-version=$azapiversion" \
--body @<(cat <<EOF
{
"properties": {
"amount": $amount,
"category": "cost",
"notifications": {
"actual_GreaterThan_85_Percent": {
"contactEmails": [
"$emailToNotify"
],
"enabled": true,
"operator": "GreaterThan",
"threshold": "85.0"
},
"actual_GreaterThan_95_Percent": {
"contactEmails": [
"$emailToNotify"
],
"enabled": true,
"operator": "GreaterThan",
"threshold": "95.0"
}
},
"timeGrain": "BillingMonth",
"timePeriod": {
"endDate": "$enddate",
"startDate": "$startdate"
}
}
}
EOF
)
done
@TiloGit
Copy link
Author

TiloGit commented May 8, 2025

direct use of AZ REST API due to various issues with the AZ CLI:
Azure/azure-cli#29950
Azure/azure-cli#31435

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment