Search code examples
jsonazureazure-deploymentazure-rm-templateazure-runbook

How to add a Runbook action to a Metric rule (classic) using Json Template Azure?


Im having an issue about how to add a Runbook action to my metric alert, there is a lot of dosumentation about sending email to owner, but none of them tells about an action Runbook.

This is my template to create a Metric alert:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "AlertName": {
      "type": "string"
    },
    "Description": {
      "type": "string"
    },
    "VirtualMachineId": {
      "type": "string"
    },
    "MetricName": {
      "type": "string"
    },
    "Operator": {
      "type": "string"
    },
    "Threshold": {
      "type": "string"
    },
    "Aggregation": {
      "type": "string"
    },
    "WindowSize": {
      "type": "string"
    }
  },
  "variables": {
  },
  "resources": [
    {
      "type": "microsoft.insights/alertRules",
      "name": "[parameters('AlertName')]",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-03-01",
      "properties": {
        "name": "[parameters('AlertName')]",
        "description": "[parameters('Description')]",
        "isEnabled": "true",
        "windowSize": "[parameters('WindowSize')]",
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[resourceId('Microsoft.Compute/virtualMachines',parameters('VirtualMachineId'))]",
            "metricName": "[parameters('MetricName')]"
          },
          "operator": "[parameters('Operator')]",
          "threshold": "[parameters('Threshold')]",
          "windowSize": "[parameters('WindowSize')]",
          "timeAggregation": "[parameters('Aggregation')]"
        },
        "actions": [
          {
            "odata.type": "RuleAction"
            //Runbook....
          }
        ]
      }
    }
  ]
}

There is no documentation about adding a RuleAction and im stuck at this point after creating a virtual machine with a Json Template.

Thank you.


Solution

  • You could follow the steps below.

    1.Navigate to your Runbook in the portal, go to Webhooks -> Add Webhook, create a new webhook and copy the URL. For more details, refer to this link.

    2.In your template, add the sample below to the actions, and specify the webhookUrl with your webhook URL , refer to this link.

    "actions": [
                    {
                        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
                        "sendToServiceOwners": "[variables('sendToServiceOwners')]",
                        "customEmails": "[variables('customEmails')]"
                    },
                    {
                        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleWebhookAction",
                        "serviceUri": "[variables('webhookUrl')]",
                        "properties": {}
                    }
                ]
    

    Create the alert and check it in the portal, it works fine on my side.

    enter image description here