Creating Logic Apps Classic Alerts with Powershell

Have you ever created a logic apps solution – maybe 10 or so logic apps – and noticed that you needed to enable basic notification alerts for all of them? I found a while ago that this was kind of a tedious process, so I end up creating a PowerShell script for that. I end up forgetting that I never blogged about that, so here it goes.

Creating Classic Alert templates

The PowerShell script I’ve end up creating uses the New-AzureRmResourceGroupDeployment command to deploy a new alert – which means I can define my alert as an ARM template. In my case why was interested in alerts if the RunFailurePercentage was bigger than 0 (e.g. any failure happened) in the last 5 minutes. So my ARM template looked like this:

{
   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0.0.0",
   "parameters": {
	 "logicappname": { "type": "string" },
	 "location": { "type": "string" },
	 "resourceid": { "type": "string"},
	 "emaillist": { "type" : "string" }
   },
   "variables": {
	 "alertName": "[concat('EN ',take(parameters('logicappname'),29))]",
	 "alertDescription": "[concat('Exception Notification for ', parameters('logicappname'))]",
	 "resourceTag": "[concat('hidden-link:',parameters('resourceid'))]"
   },
   "resources": [
	{
		"comments": "Creates an Exception Notification when error ocurrs",
		"type": "microsoft.insights/alertrules",
		"name": "[variables('alertName')]",
		"apiVersion": "2014-04-01",
		"location": "[parameters('location')]",
		"tags": {
			"[variables('resourceTag')]": "Resource"
		},
		"properties": {
			"name": "[variables('alertName')]",
			"description": "[variables('alertDescription')]",
			"isEnabled": true,
			"condition": {
				"odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
				"dataSource": {
					"odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
					"resourceUri": "[parameters('resourceid')]",
					"metricName": "RunFailurePercentage"
				},
				"operator": "GreaterThan",
				"threshold": 0,
				"windowSize": "PT5M"
			},
			"action": {
				"odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
				"sendToServiceOwners": false,
				"customEmails": [
					"[parameters('emaillist')]"
				]
			}
		},
		"resources": [],
		"dependsOn": []
	}
   ]
 }

The ARM template above receive four parameters:

  • logicappname – the name of the logic app that the alert will be attached to – this is used to create a unique name for the alert;
  • location – the location where the alert will be deployed to – ideally, the same location as the logic app;
  • resourceid – the unique id of the logic app that the alert will be attached to;
  • emaillist – a list of emails that should be alerted when the logic app is triggered.

Notice that I am using a metricName of RunFailurePercentage, which will be triggered if any Failure (operator equals to GreaterThan, thresholds equals to 0) happens in the last 5 minutes (windowsSize equals to PT5M).

Wrapping the template with PowerShell

Now that I have a ARM template, to execute this to each logic app in a resource group I just need to rub some PowerShell on it. The script looks like this:

param([string] $subscriptioname, [string] $resourcegroup, [string]$emaillist)

Get-AzureRmSubscription -SubscriptionName $subscriptioname | Select-AzureRmSubscription

Find-AzureRmResource -ResourceGroupNameContains $resourcegroup -ResourceType Microsoft.Logic/workflows | ForEach-Object {

    $parameterList = @{logicappname=$_.ResourceName;location=$_.Location;resourceid=$_.ResourceId;emaillist=$emaillist}

    New-AzureRmResourceGroupDeployment -ResourceGroupName $_.ResourceGroupName -TemplateFile LogicAppsExceptionAlertCreation.json -TemplateParameterObject $parameterList
}

This script is similar to the one I’ve used before to setup the state of each logic app in a resource group. in summary it will select the correct subscription, find all Logic Apps resources in a resource group, then will loop through each logic app and execute two lines:

  • set a parameter list with the logicapp name, resrouce id, location, and the email list;
  • execute the ARM template defined in the previous step.

Improvements

There are a some improvements that we can do to this script, which I didn’t have time to complete yet:

  • Create a set of ARM templates for different types of common actions (e.g. spike in billable actions, more complex thresholds). You could have those templates and apply each one of them by simply extracting the template name as a parameter too.
  • Create different types of actions – I’ve noticed that if you want to send a notification to owner/contributors of a logic app, you just need to toggle “sendtoServiceOwners” to true. But I couldn’t generate the webhook or execute logic apps actions templates yet (seems like the script automation in Azure can’t deal with those options). This is something to investigate.

If you want a copy of those scripts, or want to contribute with more templates or improvements, feel free to fork the repository here. I will add to it, including some documentation, or at least a link to this post, when I have some time.


Posted

in

,

by

Comments

One response to “Creating Logic Apps Classic Alerts with Powershell”

  1. […] That will help you to avoid unwanted billing and stop rogue processes earlier. You could use this powershell script to create alerts for all your logic apps in a resource […]

Leave a Reply

Your email address will not be published. Required fields are marked *