Gabriel Mora is a Software Developer at Green House Data. Connect with him on LinkedIn.
Alert Rules in Azure are a tool to let you know when some condition of your choice has occurred within any given component of your Azure infrastructure. In other words, they alert you to potential problems so you can remedy them before anything serious goes wrong.
Have you ever had the tedious task of creating multiple alerts for all of the resources in your subscription? Let me tell you, it is really time consuming to create them from scratch one by one.
I have a PowerShell Script that can Target and Create specific metric alerts for the resources you define inside of the script, making it much simpler to create a large amount of alerts at one time.
Skip down to the script if you’re familiar with Alerts already. If you aren’t here’s an overview on how they work.
Alert Rules define the target resource that will be monitored, subject that resource to a logic test when a signal is sent, and then pass it on to either an action group, with associated actions to take, or an alert state.
The target resource can be anything within your Azure environment: VMs, storage, scale sets, Log Analytics workspaces, or Application Insights. You can also select multiple resources as a single rule target.
Alert Rules are highly configurable so you can set up a wide variety of Criteria, designate the Severity from 0 to 4, and set up Action Groups accordingly. Criteria is a combination of the signal from the resource and logic, such as a certain % threshold on your CPU or a long server response time.
So, to set up a single Alert Rule, you’ll have to pick the target, select an available signal, and specify the logic that will be used for the signal data. Then you’ll have to either associate it with an Action Group or a Monitor condition. Time consuming, to be sure.
First make sure you have imported the following modules into PowerShell:
Place them in a local folder:
Now let's take a look at the script itself. Follow this link to download it from Github or click on the image below.
PLEASE NOTE: this portion of the code targets only Virtual Machines in its current state.
If you want to target additional resource types, follow this link to view all the supported metrics for Azure Monitor. Choose the resource type and metric and make the desired changes inside of the script as indicated below.
Change this section to target a different resource type and metric:
Now let’s see the progress in real time!
After the script finishes go to the Deployments section of the resource group you defined, you will see each individual job being deployed:
Creating Azure Alert Rules this way will save you lots of time. This code is highly flexible and can be easily modified. If you would like a more complex alert, simply change the parameters and template JSON, update the new values in the script, and fire away!