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.

Revisions

  1. TiloGit created this gist May 8, 2025.
    62 changes: 62 additions & 0 deletions az-create-budget-alert.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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