Kai G
Kai G

Reputation: 3492

Azure Resource Manager alarm creation

I'm trying to use an Azure Resource Manager template to create an alert in Application Insights. The problem I'm having is what value I should put for resourceUri. I've tried a few different values, I'm not sure if it's supposed to be the resource I'm monitoring or something else. The documentation is most unhelpful. When I try with the value below it gives me a validation error.

It's also not clear how I actually associate the alert with the component. Is it supposed to be nested as a resource within the component? I have a dependsOn referencing the component but from what I understand that would just ensure the other resource gets created first.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "apiVersion": "2014-04-01",
            "type": "Microsoft.Insights/components",
            "name": "testmetrics",
            "location": "Central US"
        },
        {
            "apiVersion": "2014-04-01",
            "type": "Microsoft.Insights/alertrules",
            "name": "testAlert1",
            "dependsOn": [
                "[concat('Microsoft.Insights/components/', 'testmetrics')]"
            ],
            "location": "Central US",
            "properties": {
                "description": "Test description",
                "action": {
                    "customEmails": [ "[email protected]" ]
                },
                "condition": {
                    "failedLocationCount": "1",
                    "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
                    "threshold": "0",
                    "dataSource": {
                        "metricName": "BackupFailed",
                        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
                        "resourceUri": "/Microsoft.Web/sites/mytestsite"
                    },
                    "operator": "GreaterThan",
                    "windowSize": "1"
                }
            }
        }
    ]
}

Upvotes: 2

Views: 486

Answers (2)

Ori Zohar
Ori Zohar

Reputation: 126

A great way to figure out how to write these templates correctly (if you can't find a reference in the Github repository for ARM quickstart templates) is to create a resource group in the Azure portal, configure your system and then export to a JSON template (found in the "Setting" blade when you click your resource group).

I just created an example Application Insights resource with an alert and got the one below.

You can see how the dependency is nested and the correct syntax. Also note that location for Central US is provided as "centralus"

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "alertrules_analertname_name": {
        "defaultValue": "analertname",
        "type": "String"
    },
    "components_appinsightname_name": {
        "defaultValue": "appinsightname",
        "type": "String"
    }
},
"variables": {},
"resources": [
    {
        "comments": "Generalized from resource: '/subscriptions/SOME-SUBSCRIPTIN-GUID/resourceGroups/Default-ApplicationInsights-CentralUS/providers/microsoft.insights/alertrules/analertname'.",
        "type": "microsoft.insights/alertrules",
        "name": "[parameters('alertrules_analertname_name')]",
        "apiVersion": "2014-04-01",
        "location": "East US",
        "tags": {
            "hidden-link:/subscriptions/SOME-SUBSCRIPTIN-GUID/resourcegroups/Default-ApplicationInsights-CentralUS/providers/microsoft.insights/components/appinsightname": "Resource"
        },
        "properties": {
            "name": "[parameters('alertrules_analertname_name')]",
            "description": "Some alert",
            "isEnabled": true,
            "condition": {
                "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
                "dataSource": {
                    "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
                    "resourceUri": "[resourceId('microsoft.insights/components', parameters('components_appinsightname_name'))]",
                    "metricName": "availability.availabilityMetric.value"
                },
                "threshold": 1,
                "windowSize": "PT5M"
            },
            "action": {
                "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
                "customEmails": [
                    "[email protected]"
                ]
            }
        },
        "dependsOn": [
            "[resourceId('microsoft.insights/components', parameters('components_appinsightname_name'))]"
        ]
    },
    {
        "comments": "Generalized from resource: '/subscriptions/SOME-SUBSCRIPTIN-GUID/resourceGroups/Default-ApplicationInsights-CentralUS/providers/microsoft.insights/components/appinsightname'.",
        "type": "microsoft.insights/components",
        "kind": "web",
        "name": "[parameters('components_appinsightname_name')]",
        "apiVersion": "2014-04-01",
        "location": "centralus",
        "tags": {},
        "properties": {
            "ApplicationId": "[parameters('components_appinsightname_name')]"
        },
        "dependsOn": []
    }
]

}

Hope this helps.

Upvotes: 0

Roman Oleynik
Roman Oleynik

Reputation: 619

The resourceUrl should refer to the Application Insights service in following format:

"resourceUri": "[concat(resourceGroup().id, '/providers/Microsoft.Insights/components/', 'testmetrics')]"

Upvotes: 2

Related Questions