How to Create Alert in Azure Monitor: Step-by-Step Guide
To create an alert in
Azure Monitor, go to the Azure portal, select Monitor, then Alerts, and click New alert rule. Define the resource, condition, action group, and alert details, then save to activate the alert.Syntax
Creating an alert rule in Azure Monitor involves these parts:
- Resource: The Azure service or resource you want to monitor.
- Condition: The specific metric or log signal that triggers the alert.
- Action Group: The notification or action to perform when the alert fires.
- Alert Details: Name, description, severity, and frequency of evaluation.
bash
az monitor metrics alert create --name <alert-name> --resource-group <resource-group> --scopes <resource-id> --condition "<metric> <operator> <threshold> avg <time-aggregation>" --description "<description>" --action <action-group-id> --severity <severity-level>
Example
This example creates a CPU usage alert for a virtual machine that triggers when CPU usage is over 80% for 5 minutes and sends an email notification.
bash
az monitor metrics alert create \ --name HighCpuAlert \ --resource-group MyResourceGroup \ --scopes /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM \ --condition "Percentage CPU > 80 avg 5m" \ --description "Alert when CPU usage is high" \ --action /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/microsoft.insights/actionGroups/MyActionGroup \ --severity 2
Output
Alert rule 'HighCpuAlert' created successfully.
Common Pitfalls
Common mistakes when creating Azure Monitor alerts include:
- Choosing the wrong resource or scope, so the alert never triggers.
- Setting thresholds too high or too low, causing missed alerts or too many alerts.
- Not configuring an action group, so no notifications are sent.
- Using incorrect metric names or operators in the condition.
Always verify resource IDs and test alerts after creation.
bash
Wrong example: az monitor metrics alert create --name Alert1 --resource-group RG --scopes /wrong/resource/id --condition "CPU > 90 avg 5m" --action /actionGroupId --severity 3 Right example: az monitor metrics alert create --name Alert1 --resource-group RG --scopes /subscriptions/xxxx/resourceGroups/RG/providers/Microsoft.Compute/virtualMachines/VM1 --condition "Percentage CPU > 90 avg 5m" --action /subscriptions/xxxx/resourceGroups/RG/providers/microsoft.insights/actionGroups/ActionGroup1 --severity 3
Quick Reference
| Parameter | Description | Example |
|---|---|---|
| --name | Name of the alert rule | HighCpuAlert |
| --resource-group | Resource group of the monitored resource | MyResourceGroup |
| --scopes | Resource ID(s) to monitor | /subscriptions/.../resourceGroups/.../providers/.../resourceName |
| --condition | Metric condition to trigger alert | Percentage CPU > 80 avg 5m |
| --action | Action group ID for notifications | /subscriptions/.../actionGroups/MyActionGroup |
| --severity | Alert severity level (0-4) | 2 |
| --description | Description of the alert | Alert when CPU usage is high |
Key Takeaways
Create alerts by defining resource, condition, action group, and alert details in Azure Monitor.
Use the Azure CLI or portal to set up alerts with clear thresholds and notification actions.
Verify resource IDs and metric names to avoid alert misconfiguration.
Always configure an action group to receive notifications when alerts trigger.
Test alerts after creation to ensure they work as expected.