Create a new alert rule using the CLI, PowerShell, or an ARM template
You can create a new alert rule using the the CLI, PowerShell, or an Azure Resource Manager template.
Prerequisites
To create or edit an alert rule, you must have the following permissions:
- Read permission on the target resource of the alert rule.
- Write permission on the resource group in which the alert rule is created. If you're creating the alert rule from the Azure portal, the alert rule is created by default in the same resource group in which the target resource resides.
- Read permission on any action group associated to the alert rule, if applicable.
Create a new alert rule using the CLI
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}
Create a new alert rule using PowerShell
- 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 New-AzActivityLogAlert cmdlet.
Create a new alert rule using an ARM template
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.