1
votes

I am creating application insights with new metric rules. I was previously using classic alerts and was working fine before sep 1, 2019. I want to adopt to new alerts so I am modifying my arm template below:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "applicationinsights_platform": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 6
          },
          "applicationinsights_location_shortname": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 3
          },
          "applicationinsights_project": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 6
          },
          "applicationinsights_environment": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 7
          },
          "applicationinsights_uniqueid": {
            "defaultValue": "1",
            "type": "string",
            "maxLength": 1
          },
          "applicationinsights_componentKind": {
            "type": "string",
            "allowedValues": [
              "web",
              "ios",
              "other",
              "store",
              "java",
              "phone"
            ],
            "defaultValue": "web",
            "metadata": {
              "description": "application insights component kind"
            }
          },   
            "applicationinsights_alertDescription": {
            "type": "string",
            "defaultValue": "This is a metric alert",
            "metadata": {
                "description": "Description of alert"
            }
        },
        "applicationinsights_alertSeverity": {
            "type": "int",
            "defaultValue": 3,
            "allowedValues": [
                0,
                1,
                2,
                3,
                4
            ],
            "metadata": {
                "description": "Severity of alert {0,1,2,3,4}"
            }
        },
        "applicationinsights_isEnabled": {
            "type": "bool",
            "defaultValue": true,
            "metadata": {
                "description": "Specifies whether the alert is enabled"
            }
        },
            "applicationinsights_metricName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "Name of the metric used in the comparison to activate the alert."
            }
        },
        "applicationinsights_operator": {
            "type": "string",
            "defaultValue": "GreaterThan",
            "allowedValues": [
                "Equals",
                "NotEquals",
                "GreaterThan",
                "GreaterThanOrEqual",
                "LessThan",
                "LessThanOrEqual"
            ],
            "metadata": {
                "description": "Operator comparing the current value with the threshold value."
            }
        },
        "applicationinsights_threshold": {
            "type": "string",
            "defaultValue": "0",
            "metadata": {
                "description": "The threshold value at which the alert is activated."
            }
        },
        "applicationinsights_timeAggregation": {
            "type": "string",
            "defaultValue": "Average",
            "allowedValues": [
                "Average",
                "Minimum",
                "Maximum",
                "Total",
                "Count"
            ],
            "metadata": {
                "description": "How the data that is collected should be combined over time."
            }
        },
        "applicationinsights_windowSize": {
            "type": "string",
            "defaultValue": "PT5M",
            "allowedValues": [
                "PT1M",
                "PT5M",
                "PT15M",
                "PT30M",
                "PT1H",
                "PT6H",
                "PT12H",
                "PT24H"
            ],
            "metadata": {
                "description": "Period of time used to monitor alert activity based on the threshold. Must be between one minute and one day. ISO 8601 duration format."
            }
        },
        "evaluationFrequency": {
            "type": "string",
            "defaultValue": "PT1M",
            "allowedValues": [
                "PT1M",
                "PT5M",
                "PT15M",
                "PT30M",
                "PT1H"
            ],
            "metadata": {
                "description": "how often the metric alert is evaluated represented in ISO 8601 duration format"
            }
        },
         "applicationinsights_instrumentation_key_output": {
          "defaultValue": "ApplicationInsightsInstrumentationKey",
          "type": "string"
        },

        "applicationinsights_actionGroupId": {
            "type": "string",
            "defaultValue": "test",
            "metadata": {
                "description": "The ID of the action group that is triggered when the alert is activated or deactivated"
            }
        }
    },
    "variables": { 
      "basename": "[concat(parameters('applicationinsights_platform'), '-', parameters('applicationinsights_project'), '-', parameters('applicationinsights_location_shortname'), '-',  parameters('applicationinsights_environment'))]",
      "alertRulesName": "[concat(variables('basename'), '-aaiar-', parameters('applicationinsights_uniqueid'))]",
      "appInsightName": "[concat(variables('basename'), '-aaic-', parameters('applicationinsights_uniqueid'))]"
     },
    "resources": [
        {
            "name": "[variables('alertRulesName')]",
            "type": "Microsoft.Insights/metricAlerts",
            "location": "global",
            "apiVersion": "2018-03-01",
            "tags": {
              "[concat('hidden-link:', resourceId('Microsoft.Insights/components', variables('appInsightName')))]": "Resource"  
            },
            "properties": {
                "name": "[variables('alertRulesName')]",
                "description": "Alert rule assigned to Application Insight component",
                "severity": "[parameters('applicationinsights_alertSeverity')]",
                "enabled": "[parameters('applicationinsights_isEnabled')]",
                "scopes": ["[resourceId('microsoft.insights/components', variables('appInsightName'))]"],
                "evaluationFrequency":"[parameters('evaluationFrequency')]",
                "windowSize": "[parameters('applicationinsights_windowSize')]",
                "criteria": {
                    "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
                    "allOf": [
                        {
                            "name" : "test01",
                            "metricName": "[parameters('applicationinsights_metricName')]",
                            "dimensions":[],
                            "operator": "[parameters('applicationinsights_operator')]",
                            "threshold" : "[parameters('applicationinsights_threshold')]",
                            "timeAggregation": "[parameters('applicationinsights_timeAggregation')]"
                        }
                             ]
                           },
                "actions": [
                    {
                        "actionGroupId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'), '/providers/Microsoft.Insights/actionGroups/',parameters('applicationinsights_platform'))]"
                    }
                           ]
                         },
            "dependsOn": [
          "[resourceId('microsoft.insights/components', variables('appInsightName'))]"
                         ]
       },
     {
      "name": "[variables('appInsightName')]",
      "type": "microsoft.insights/components",
      "apiVersion": "2015-05-01",
      "location": "[resourceGroup().location]",
      "tags": {},
      "kind": "[parameters('applicationinsights_componentKind')]",
      "properties": {
        "ApplicationId": "[variables('appInsightName')]"
      }}

    ],
    "outputs": {
        "secrets": {
          "type": "object",
          "value": {
            "[parameters('applicationinsights_instrumentation_key_output')]": "[reference(concat('microsoft.insights/components/', variables('appInsightName'))).InstrumentationKey]"
          }
        }
      }  

  }

My parameter file is-

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "applicationinsights_platform": {
            "value": "abc"
        },
         "applicationinsights_location_shortname": {
            "value": "def"
        },
         "applicationinsights_project": {
            "value": "ghi"
          },
          "applicationinsights_environment": {
            "value": "ijk"
          },
          "applicationinsights_uniqueid": {
            "value": "1"
          },
          "applicationinsights_componentKind": {
            "value": "web"
          },
          "applicationinsights_metricName": {
            "value": "availabilityResults/availabilityPercentage"
          },
          "applicationinsights_operator": {
            "value": "GreaterThan"
          },
          "applicationinsights_threshold": {
            "value": "80"
          },
          "applicationinsights_windowSize": {
            "value": "PT5M"
          },
          "applicationinsights_timeAggregation": {
            "value": "Average"
          },
         "applicationinsights_alertDescription": {
            "value": "New metric alert created via template"
          },
        "applicationinsights_alertSeverity": {
            "value": 3
        },
        "applicationinsights_isEnabled": {
            "value": true
        }
    }
}

I am getting error-

"Code": "BadRequest", "Message": "Arm resource /subscriptions/xxxx/resourceGroups/abc-def-ghi-jkl/providers/Microsoft.Insights/actionGroups/abc is invalid."

My Resource group is already existing and I am using Az Cli (Version 2.0.64) and below command to create the resource-

az group deployment create --name 'test' --resource-group 'abc-def-ghi-jkl' --template-file azuredeploy.json --parameters azuredeploy.parameter.json --verbose --debug

However the resources are provisioned when I hardcode the part in template-

 "actions": [
                    {
                        "actionGroupId": "/subscriptions/xxxxxxxx/resourceGroups/abc-def-ghi-jkl/providers/Microsoft.Insights/actionGroups/test0"
                    }
                           ]

I am not getting any clue why is it throwing an error when I am parameterising it. I took reference for ARM template from here - https://docs.microsoft.com/en-us/azure/templates/microsoft.insights/2018-03-01/metricalerts

Thanks in advance for your help/suggestions.

2

2 Answers

1
votes

I understood later that the action group needs to be added at first and then only it can be linked to some other resource. So I added another resource actiongroup and then I was able to create app insight without any problem. Yeah, The only thing you should take care is AG short name shouldn't exceed 12 characters.

Here is the ARM template -

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "applicationinsights_platform": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 6
    },
    "applicationinsights_location_shortname": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 3
    },
    "applicationinsights_project": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 6
    },
    "applicationinsights_environment": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 7
    },
    "applicationinsights_uniqueid": {
      "defaultValue": "1",
      "type": "string",
      "maxLength": 1
    },
    "applicationinsights_componentKind": {
      "type": "string",
      "allowedValues": [
        "web",
        "ios",
        "other",
        "store",
        "java",
        "phone"
      ],
      "defaultValue": "web",
      "metadata": {
        "description": "application insights component kind"
      }
    },
    "applicationinsights_alertSeverity": {
      "type": "int",
      "defaultValue": 3,
      "allowedValues": [
        0,
        1,
        2,
        3,
        4
      ],
      "metadata": {
        "description": "Severity of alert {0,1,2,3,4}"
      }
    },
    "applicationinsights_metricName": {
      "type": "string",
      "minLength": 1,
      "metadata": {
        "description": "Name of the metric used in the comparison to activate the alert."
      }
    },
    "applicationinsights_operator": {
      "type": "string",
      "defaultValue": "GreaterThan",
      "allowedValues": [
        "Equals",
        "NotEquals",
        "GreaterThan",
        "GreaterThanOrEqual",
        "LessThan",
        "LessThanOrEqual"
      ],
      "metadata": {
        "description": "Operator comparing the current value with the threshold value."
      }
    },
    "applicationinsights_threshold": {
      "type": "string",
      "defaultValue": "0",
      "metadata": {
        "description": "The threshold value at which the alert is activated."
      }
    },
    "applicationinsights_timeAggregation": {
      "type": "string",
      "defaultValue": "Average",
      "allowedValues": [
        "Average",
        "Minimum",
        "Maximum",
        "Total",
        "Count"
      ],
      "metadata": {
        "description": "How the data that is collected should be combined over time."
      }
    },
    "applicationinsights_windowSize": {
      "type": "string",
      "defaultValue": "PT5M",
      "allowedValues": [
        "PT1M",
        "PT5M",
        "PT15M",
        "PT30M",
        "PT1H",
        "PT6H",
        "PT12H",
        "PT24H"
      ],
      "metadata": {
        "description": "Period of time used to monitor alert activity based on the threshold. Must be between one minute and one day. ISO 8601 duration format."
      }
    },
    "evaluationFrequency": {
      "type": "string",
      "defaultValue": "PT1M",
      "allowedValues": [
        "PT1M",
        "PT5M",
        "PT15M",
        "PT30M",
        "PT1H"
      ],
      "metadata": {
        "description": "how often the metric alert is evaluated represented in ISO 8601 duration format"
      }
    },
    "applicationinsights_instrumentation_key_output": {
      "defaultValue": "ApplicationInsightsInstrumentationKey",
      "type": "string"
    },
    "applicationinsights_actionGroupId": {
      "type": "string",
      "defaultValue": "test",
      "metadata": {
        "description": "The ID of the action group that is triggered when the alert is activated or deactivated"
      }
    },
    "applicationinsights_recieverEmailId": {
      "type": "string",
      "defaultValue": "[email protected]",
      "metadata": {
        "description": "Email Address of Receiver"
      }
    },
    "applicationinsights_webhookURI": {
      "type": "string",
      "defaultValue": "http://requestb.in/1bq62iu2",
      "metadata": {
        "description": "Webhook receiver service URI"
      }
    }
  },
  "variables": {
    "basename": "[concat(parameters('applicationinsights_platform'), '-', parameters('applicationinsights_project'), '-', parameters('applicationinsights_location_shortname'), '-',  parameters('applicationinsights_environment'))]",
    "alertRulesName": "[concat(variables('basename'), '-aiar-', parameters('applicationinsights_uniqueid'))]",
    "appInsightName": "[concat(variables('basename'), '-aic-', parameters('applicationinsights_uniqueid'))]",
    "AGName": "[concat(parameters('applicationinsights_platform'), parameters('applicationinsights_project'), parameters('applicationinsights_location_shortname'), parameters('applicationinsights_environment'))]",
    "AGId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'),'/providers/Microsoft.Insights/actionGroups/',variables('AGName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Insights/actionGroups",
      "apiVersion": "2018-03-01",
      "name": "[variables('AGName')]",
      "location": "Global",
      "properties": {
        "groupShortName": "[variables('AGName')]",
        "enabled": true,
        "emailReceivers": [
          {
            "name": "EmailOfRecipient",
            "emailAddress": "[parameters('applicationinsights_recieverEmailId')]"
          }
        ],
        "webhookReceivers": [
          {
            "name": "WebhookURI",
            "serviceUri": "[parameters('applicationinsights_webhookURI')]"
          }
        ]
      }
    },
    {
      "name": "[variables('alertRulesName')]",
      "type": "Microsoft.Insights/metricAlerts",
      "location": "global",
      "apiVersion": "2018-03-01",
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Insights/components', variables('appInsightName')))]": "Resource"
      },
      "properties": {
        "name": "[variables('alertRulesName')]",
        "description": "Alert rule assigned to Application Insight component",
        "severity": "[parameters('applicationinsights_alertSeverity')]",
        "enabled": true,
        "scopes": [
          "[resourceId('microsoft.insights/components', variables('appInsightName'))]"
        ],
        "evaluationFrequency": "[parameters('evaluationFrequency')]",
        "windowSize": "[parameters('applicationinsights_windowSize')]",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
          "allOf": [
            {
              "name": "[variables('AGName')]",
              "metricName": "[parameters('applicationinsights_metricName')]",
              "dimensions": [],
              "operator": "[parameters('applicationinsights_operator')]",
              "threshold": "[parameters('applicationinsights_threshold')]",
              "timeAggregation": "[parameters('applicationinsights_timeAggregation')]"
            }
          ]
        },
        "actions": [
          {
            "actionGroupId": "[variables('AGId')]"
          }
        ]
      },
      "dependsOn": [
        "[resourceId('microsoft.insights/components', variables('appInsightName'))]",
        "[resourceId('microsoft.insights/actionGroups', variables('AGName'))]"
      ]
    },
    {
      "name": "[variables('appInsightName')]",
      "type": "microsoft.insights/components",
      "apiVersion": "2015-05-01",
      "location": "[resourceGroup().location]",
      "tags": {},
      "kind": "[parameters('applicationinsights_componentKind')]",
      "properties": {
        "ApplicationId": "[variables('appInsightName')]"
      }
    }
  ],
  "outputs": {
    "secrets": {
      "type": "object",
      "value": {
        "[parameters('applicationinsights_instrumentation_key_output')]": "[reference(concat('microsoft.insights/components/', variables('appInsightName'))).InstrumentationKey]"
      }
    }
  }
}

and my parameter file-

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "applicationinsights_platform": {
      "value": "foo"
    },
    "applicationinsights_location_shortname": {
      "value": "eus"
    },
    "applicationinsights_project": {
      "value": "bar"
    },
    "applicationinsights_environment": {
      "value": "dev"
    },
    "applicationinsights_uniqueid": {
      "value": "1"
    },
    "applicationinsights_componentKind": {
      "value": "web"
    },
    "applicationinsights_metricName": {
      "value": "requests/duration"
    },
    "applicationinsights_operator": {
      "value": "GreaterThan"
    },
    "applicationinsights_threshold": {
      "value": "10000"
    },
    "applicationinsights_windowSize": {
      "value": "PT5M"
    },
    "applicationinsights_timeAggregation": {
      "value": "Average"
    },
    "applicationinsights_alertSeverity": {
      "value": 3
    },
    "applicationinsights_recieverEmailId": {
      "value": "[email protected]"
    },
    "applicationinsights_webhookURI": {
      "value": "https://requestb.in/1bq62iu2"
    }
  }
}
0
votes

Looks like there is a typo in your ARM template. You wrote:

"actions": [
                {
                    "actionGroupId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'), '/providers/Microsoft.Insights/actionGroups/',parameters('applicationinsights_platform'))]"
                }

But looking at your parameters, your probably meant to use applicationinsights_actionGroupId instead of applicationinsights_platform as the action group name. Like this:

"actionGroupId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'), '/providers/Microsoft.Insights/actionGroups/',parameters('applicationinsights_actionGroupId'))]"

As a suggestion, you can also create the action group in your ARM template (see here for the documentation), and then set the action group id with a resourceId reference:

"actionGroupId": "[resourceId('Microsoft.Insights/actionGroups',parameters('applicationinsights_actionGroupId'))]"

Hope this helps,

Asaf