How to enable Azure "Daily anomaly by resource group" alert using ARM template?

72 Views Asked by At

I have Anomaly Cost Detection alert created in Azure Subscription -> Cost Management -> Cost Alerts. The alert was created manually via Azure Portal. As I have created an ARM template that will deploy few other "standard Azure Monitor" alerts upon creation of Azure Subscription, I want to find a way to enable/disable this alert using ARM template so that I can include it in the list.

What I understood is that the alert is extension and cannot exist as a resource by itself, and the documentation states that needs to be attached to other resource. Does anyone have an idea to which one? Also, the documentation states "To create a Microsoft.CostManagement/alerts resource, add the following JSON to your template.", can anyone suggest a template where I can indeed add this resource? MS Docs Link: https://learn.microsoft.com/en-us/azure/templates/microsoft.costmanagement/2019-11-01/alerts?pivots=deployment-language-arm-template

1

There are 1 best solutions below

1
Vinay B On

Effective plan for Cost Alert Management with ARM Templates

To create a Microsoft.CostManagement/alerts resource for the test purpose, I fetch the template snapshot from my resource group.

enter image description here

You’ll need to add the following snippet to your ARM template:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.CostManagement/alerts",
            "apiVersion": "2022-10-05-preview",
            "name": "MyAnomalyAlert",
            "existing": {
                "name": "vinay-rg" 
            },
            "properties": {                         
            }
        }
    ]
}

Replace the alert name and resource group name from the that associate with the alert.

The "properties" section allows you to configure the alert further. You can specify properties such as thresholds, frequency, and actions to take when the alert triggers.

Save the template with alerts as azuredeploy.json

now run the commands

az deployment group create --resource-group vinay-rg --template-file azuredeploy.json

Once the deployment is successful, navigate to the Azure portal Go to the resource group where you deployed the template Verify that the Cost Management alert is associated with the specified resource group.

Reference:

https://learn.microsoft.com/en-us/azure/templates/microsoft.costmanagement/alerts?pivots=deployment-language-terraform

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/export-template-portal

https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/resource-manager-alerts-log?tabs=json