Metric Alert
Introduction
Section titled “Introduction”Azure Monitor Metric Alerts trigger notifications when a monitored metric crosses a defined threshold. They evaluate metric data at configurable intervals and route notifications through Action Groups when conditions are met. Metric Alerts are commonly used to detect anomalies in CPU usage, memory pressure, request latency, and other resource metrics across Azure workloads. For more information, see Overview of metric alerts in Azure Monitor.
LocalStack for Azure provides a local environment for building and testing applications that make use of Azure Monitor Metric Alerts. The supported APIs are available on our API Coverage section, which provides information on the extent of Metric Alerts’ integration with LocalStack.
Getting started
Section titled “Getting started”This guide walks you through creating a metric alert rule referencing an action group.
Launch LocalStack using your preferred method. For more information, see Introduction to LocalStack for Azure. Once the container is running, enable Azure CLI interception by running:
azlocal start-interceptionThis command points the az CLI away from the public Azure management REST API and toward the LocalStack for Azure emulator API.
To revert this configuration, run:
azlocal stop-interceptionThis reconfigures the az CLI to send commands to the official Azure management REST API.
Create a resource group
Section titled “Create a resource group”Create a resource group to hold all resources created in this guide:
az group create --name rg-alert-demo --location westeurope{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo", "location": "eastus", "name": "rg-alert-demo", "properties": { "provisioningState": "Succeeded" }, "type": "Microsoft.Resources/resourceGroups"}Create an action group
Section titled “Create an action group”Create an action group to use as the notification target for the metric alert:
az monitor action-group create \ --name my-ag \ --resource-group rg-alert-demo \ --short-name myag \ --action email admin admin@example.com{ "groupShortName": "myag", "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/microsoft.insights/actionGroups/my-ag", "name": "my-ag", "resourceGroup": "rg-alert-demo", "type": "Microsoft.Insights/ActionGroups"...}Create a metric alert
Section titled “Create a metric alert”The following alert fires when CPU percentage on a virtual machine exceeds 80%:
SCOPE="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Compute/virtualMachines/my-vm"AG_ID="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/microsoft.insights/actionGroups/my-ag"
az monitor metrics alert create \ --name cpu-alert \ --resource-group rg-alert-demo \ --scopes "$SCOPE" \ --condition "avg Percentage CPU > 80" \ --action "$AG_ID" \ --description "Alert when CPU > 80%"{ "criteria": { "allOf": [ { "criterionType": "StaticThresholdCriterion", "metricName": "Percentage CPU", "name": "cond0", "operator": "GreaterThan", "threshold": 80.0, "timeAggregation": "Average" } ], "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria" }, "description": "Alert when CPU > 80%", "enabled": true, "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/metricAlerts/cpu-alert", "name": "cpu-alert", "resourceGroup": "rg-alert-demo", "severity": 2, "type": "Microsoft.Insights/metricAlerts", ...}Show and list metric alerts
Section titled “Show and list metric alerts”Retrieve the details of the metric alert and list all alerts in the resource group:
az monitor metrics alert show \ --name cpu-alert \ --resource-group rg-alert-demo{ "criteria": { "allOf": [ { "metricName": "Percentage CPU", "operator": "GreaterThan", "threshold": 80.0, "timeAggregation": "Average", ... } ], "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria" }, "description": "Alert when CPU > 80%", "enabled": true, "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/metricAlerts/cpu-alert", "name": "cpu-alert", "resourceGroup": "rg-alert-demo", "severity": 2, "type": "Microsoft.Insights/metricAlerts"...}Then list all metric alerts in the resource group:
az monitor metrics alert list \ --resource-group rg-alert-demo[ { "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/metricAlerts/cpu-alert", "name": "cpu-alert", "resourceGroup": "rg-alert-demo", "severity": 2, "type": "Microsoft.Insights/metricAlerts" }]Create an activity log alert
Section titled “Create an activity log alert”Create an activity log alert that fires when a virtual machine is deleted in the resource group:
az monitor activity-log alert create \ --name service-health-alert \ --resource-group rg-alert-demo \ --scope "/subscriptions/00000000-0000-0000-0000-000000000000" \ --condition category=ServiceHealth \ --action-group "$AG_ID"{ "actions": { "actionGroups": [ { "actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/microsoft.insights/actionGroups/my-ag" } ] }, "condition": { "allOf": [ { "equals": "ServiceHealth", "field": "category" } ] }, "enabled": true, "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/activityLogAlerts/service-health-alert", "name": "service-health-alert", "resourceGroup": "rg-alert-demo", "scopes": ["/subscriptions/00000000-0000-0000-0000-000000000000"], "type": "Microsoft.Insights/ActivityLogAlerts"...}Delete and verify
Section titled “Delete and verify”Delete the resource and confirm it no longer appears in the list:
az monitor metrics alert delete \ --name cpu-alert \ --resource-group rg-alert-demoThen list all metric alerts to confirm the resource group is now empty:
az monitor metrics alert list --resource-group rg-alert-demo[]Features
Section titled “Features”- Metric alert lifecycle: Create, read, list, update, and delete metric alert rules.
- Activity log alert lifecycle: Create, read, list, and delete activity log alert rules.
- Single and multi-resource scopes: Define alerts scoped to a single resource or multiple resources.
- Action group references: Associate action groups with alert rules.
- Dynamic threshold support: Accept dynamic threshold criteria in the alert condition.
- Severity configuration: Set alert severity from 0 (Critical) to 4 (Verbose).
- Auto-mitigation: Configure whether alerts auto-resolve when the condition clears.
- Frequency and window size: Configure evaluation frequency and aggregation window size.
Limitations
Section titled “Limitations”- No alert evaluation: Metric conditions are not evaluated against real or simulated metric data. Alerts never fire.
- No state transitions: Alert state (Fired, Resolved) is not tracked or updated.
- No email or webhook dispatch: Even if an alert were to fire, no notifications would be sent.
- Metrics not ingested: Metric data is not ingested, stored, or queryable from LocalStack.
Samples
Section titled “Samples”Explore end-to-end examples in the LocalStack for Azure Samples repository.
API Coverage
Section titled “API Coverage”| Operation ▲ | Implemented ▼ |
|---|