title | description | author | ms.author | ms.topic | ms.custom | ms.date | ms.reviewer |
---|---|---|---|---|---|---|---|
Create Azure Monitor alert rules using the CLI, PowerShell or an ARM template |
This article shows you how to create a new alert rule using the CLI, PowerShell or an ARM template. |
AbbyMSFT |
abbyweisberg |
how-to |
devx-track-arm-template |
01/03/2024 |
harelbr |
You can create a new alert rule using the the CLI, PowerShell, or an Azure Resource Manager template.
You can create a new alert rule using the Azure CLI. The following code examples use Azure Cloud Shell. You can see the full list of the Azure CLI commands for Azure Monitor.
-
In the portal, select Cloud Shell. At the prompt, use these.
- To create a metric alert rule, use the az monitor metrics alert create command.
- To create a log search alert rule, use the az monitor scheduled-query create command.
- To create an activity log alert rule, use the az monitor activity-log alert create command.
For example, to create a metric alert rule that monitors if average Percentage CPU on a VM is greater than 90:
az monitor metrics alert create -n {nameofthealert} -g {ResourceGroup} --scopes {VirtualMachineResourceID} --condition "avg Percentage CPU > 90" --description {descriptionofthealert}
- To create a metric alert rule using PowerShell, use the Add-AzMetricAlertRuleV2 cmdlet.
[!NOTE] When you create a metric alert on a single resource, the syntax uses the
TargetResourceId
. When you create a metric alert on multiple resources, the syntax contains theTargetResourceScope
,TargetResourceType
, andTargetResourceRegion
. - To create a log search alert rule using PowerShell, use the New-AzScheduledQueryRule cmdlet.
- To create an activity log alert rule using PowerShell, use the Set-AzActivityLogAlert cmdlet.
You can use an Azure Resource Manager template (ARM template) to configure alert rules consistently in all of your environments.
-
Create a new resource, using the following resource types:
- For metric alerts:
Microsoft.Insights/metricAlerts
[!NOTE]
- We recommend that you create the metric alert using the same resource group as your target resource.
- Metric alerts for an Azure Log Analytics workspace resource type (
Microsoft.OperationalInsights/workspaces
) are configured differently than other metric alerts. For more information, see Resource Template for Metric Alerts for Logs. - If you are creating a metric alert for a single resource, the template uses the
ResourceId
of the target resource. If you are creating a metric alert for multiple resources, the template uses thescope
,TargetResourceType
, andTargetResourceRegion
for the target resources.
- For log search alerts:
Microsoft.Insights/scheduledQueryRules
- For activity log, service health, and resource health alerts:
microsoft.Insights/activityLogAlerts
- For metric alerts:
-
Copy one of the templates from these sample ARM templates.
- For metric alerts: Resource Manager template samples for metric alert rules
- For log search alerts: Resource Manager template samples for log search alert rules
- For activity log alerts: Resource Manager template samples for activity log alert rules
- For service health alerts: Resource Manager template samples for service health alert rules
- For resource health alerts: Resource Manager template samples for resource health alert rules
-
Edit the template file to contain appropriate information for your alert, and save the file as <your-alert-template-file>.json.
-
Edit the corresponding parameters file to customize the alert, and save as <your-alert-template-file>.parameters.json.
-
Set the
metricName
parameter, using one of the values in Azure Monitor supported metrics. -
Deploy the template using PowerShell or the CLI.