Search code examples
azurealertazure-resource-managerazure-rm-templatecost-management

Make a budget alert when forcasted is reached


I have made a budget in ARM template, where I have two alert under notifications. as default the "ThresholdType" is a "actual" and if i try to change it to forecasted it gives me the error "Value must be one of the following values: "Actual""

The idea is to trigger an alert when the forcasted budget is reached. You can do this in the portal, but is it somehow possible to do this programmatically?

{
                            "type": "Microsoft.Consumption/budgets",
                            "apiVersion": "2019-10-01",
                            "name": "[parameters('budgetName')]",
                            "properties": {
                                "timePeriod": {
                                    "startDate": "[parameters('startDate')]",
                                    "endDate": "[parameters('endDate')]"
                                },
                                "timeGrain": "[parameters('timeGrain')]",
                                "amount": "[parameters('amount')]",
                                "category": "Cost",
                                "notifications": {
                                    "NotificationForExceededBudget1": {
                                        "enabled": true,
                                        "operator": "GreaterThan",
                                        "threshold": "[parameters('firstThreshold')]",
                                        "contactEmails": "[parameters('ErrorEmailReceivers')]",
                                        "contactRoles": "[parameters('contactRoles')]",
                                        "contactGroups": "[resourceId('Microsoft.Insights/actionGroups',variables('WarnAGName'))]"
                                    },
                                    "NotificationForExceededBudget2": {
                                        "enabled": true,
                                        "operator": "GreaterThan",
                                        "threshold": "[parameters('secondThreshold')]",
                                        "contactEmails": "[parameters('WarningEmailReceivers')]",
                                        "contactRoles": "[parameters('contactRoles')]",
                                        "contactGroups": "[resourceId('Microsoft.Insights/actionGroups',variables('WarnAGName'))]"
                                    }
                                },
                                "filter": {
                                    "and": [
                                        {
                                            "dimensions": {
                                                "name": "ResourceGroupName",
                                                "operator": "In",
                                                "values": "[parameters('resourceGroupFilterValues')]"
                                            }
                                        },
                                        {
                                            "dimensions": {
                                                "name": "MeterCategory",
                                                "operator": "In",
                                                "values": "[parameters('meterCategoryFilterValues')]"
                                            }
                                        }

                                    ]
                                }
                            }
                        }


Solution

  • You can use the below ARM template to create forecasted alerts. we have tested the below template in our local environment & it is working fine.

    {
        "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "budgetName": {
                "defaultValue": "MyBudget",
                "type": "String",
                "metadata": {
                    "description": "Name of the Budget. It should be unique within a resource group."
                }
            },
            "amount": {
                "defaultValue": "1000",
                "type": "String",
                "metadata": {
                    "description": "The total amount of cost or usage to track with the budget"
                }
            },
            "timeGrain": {
                "defaultValue": "Monthly",
                "allowedValues": [
                    "Monthly",
                    "Quarterly",
                    "Annually"
                ],
                "type": "String",
                "metadata": {
                    "description": "The time covered by a budget. Tracking of the amount will be reset based on the time grain."
                }
            },
            "startDate": {
                "type": "String",
                "metadata": {
                    "description": "The start date must be first of the month in YYYY-MM-DD format. Future start date should not be more than three months. Past start date should be selected within the timegrain preiod."
                }
            },
            "endDate": {
                "type": "String",
                "metadata": {
                    "description": "The end date for the budget in YYYY-MM-DD format. If not provided, we default this to 10 years from the start date."
                }
            },
            "firstThreshold": {
                "defaultValue": "90",
                "type": "String",
                "metadata": {
                    "description": "Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0 and 1000."
                }
            },
            "secondThreshold": {
                "defaultValue": "110",
                "type": "String",
                "metadata": {
                    "description": "Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0 and 1000."
                }
            },
            "contactRoles": {
                "defaultValue": [
                    "Owner",
                    "Contributor",
                    "Reader"
                ],
                "type": "Array",
                "metadata": {
                    "description": "The list of contact roles to send the budget notification to when the threshold is exceeded."
                }
            },
            "contactEmails": {
                "type": "Array",
                "metadata": {
                    "description": "The list of email addresses to send the budget notification to when the threshold is exceeded."
                }
            },
            "contactGroups": {
                "type": "Array",
                "metadata": {
                    "description": "The list of action groups to send the budget notification to when the threshold is exceeded. It accepts array of strings."
                }
            },
            "resourceGroupFilterValues": {
                "type": "Array",
                "metadata": {
                    "description": "The set of values for the first filter"
                }
            },
            "meterCategoryFilterValues": {
                "type": "Array",
                "metadata": {
                    "description": "The set of values for the second filter"
                }
            }
        },
        "variables": {},
        "resources": [
            {
                "type": "Microsoft.Consumption/budgets",
                "apiVersion": "2019-10-01",
                "name": "[parameters('budgetName')]",
                "properties": {
                    "timePeriod": {
                        "startDate": "[parameters('startDate')]",
                        "endDate": "[parameters('endDate')]"
                    },
                    "timeGrain": "[parameters('timeGrain')]",
                    "amount": "[parameters('amount')]",
                    "category": "Cost",
                    "notifications": {
                        "NotificationForExceededBudget1": {
                            "enabled": true,
                            "operator": "GreaterThan",
                            "threshold": "[parameters('firstThreshold')]",
                            "thresholdType": "Actual",
                            "contactEmails": "[parameters('contactEmails')]",
                            "contactRoles": "[parameters('contactRoles')]",
                            "contactGroups": "[parameters('contactGroups')]"
                        },
                        "NotificationForExceededBudget2": {
                            "enabled": true,
                            "operator": "GreaterThan",
                            "threshold": "[parameters('secondThreshold')]",
                            "thresholdType": "Forecasted",
                            "contactEmails": "[parameters('contactEmails')]",
                            "contactRoles": "[parameters('contactRoles')]",
                            "contactGroups": "[parameters('contactGroups')]"
                        }
                    },
                    "filter": {
                        "and": [
                            {
                                "dimensions": {
                                    "name": "ResourceGroupName",
                                    "operator": "In",
                                    "values": "[parameters('resourceGroupFilterValues')]"
                                }
                            },
                            {
                                "dimensions": {
                                    "name": "MeterCategory",
                                    "operator": "In",
                                    "values": "[parameters('meterCategoryFilterValues')]"
                                }
                            }
                        ]
                    }
                }
            }
        ]
    }
    

    Here is the sample output for reference:

    enter image description here