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:

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:

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.


Sharing is caring...

One thought on “Creating Logic Apps Classic Alerts with Powershell”

Leave a Reply

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