|
| 1 | +--- |
| 2 | +title: "Metric Alert" |
| 3 | +description: Get started with Azure Monitor Metric Alerts on LocalStack |
| 4 | +template: doc |
| 5 | +--- |
| 6 | + |
| 7 | +import AzureFeatureCoverage from "../../../../components/feature-coverage/AzureFeatureCoverage"; |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +Azure Monitor Metric Alerts trigger notifications when a monitored metric crosses a defined threshold. |
| 12 | +They evaluate metric data at configurable intervals and route notifications through Action Groups when conditions are met. |
| 13 | +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](https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-metric-overview). |
| 14 | + |
| 15 | +LocalStack for Azure provides a local environment for building and testing applications that make use of Azure Monitor Metric Alerts. |
| 16 | +The supported APIs are available on our [API Coverage section](#api-coverage), which provides information on the extent of Metric Alerts' integration with LocalStack. |
| 17 | + |
| 18 | +## Getting started |
| 19 | + |
| 20 | +This guide walks you through creating a metric alert rule referencing an action group. |
| 21 | + |
| 22 | +Launch LocalStack using your preferred method. For more information, see [Introduction to LocalStack for Azure](/azure/getting-started/). Once the container is running, enable Azure CLI interception by running: |
| 23 | + |
| 24 | +```bash |
| 25 | +azlocal start-interception |
| 26 | +``` |
| 27 | + |
| 28 | +This command points the `az` CLI away from the public Azure management REST API and toward the LocalStack for Azure emulator API. |
| 29 | +To revert this configuration, run: |
| 30 | + |
| 31 | +```bash |
| 32 | +azlocal stop-interception |
| 33 | +``` |
| 34 | + |
| 35 | +This reconfigures the `az` CLI to send commands to the official Azure management REST API. |
| 36 | + |
| 37 | +### Create a resource group |
| 38 | + |
| 39 | +Create a resource group to hold all resources created in this guide: |
| 40 | + |
| 41 | +```bash |
| 42 | +az group create --name rg-alert-demo --location westeurope |
| 43 | +``` |
| 44 | + |
| 45 | +```bash title="Output" |
| 46 | +{ |
| 47 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo", |
| 48 | + "location": "westeurope", |
| 49 | + "name": "rg-alert-demo", |
| 50 | + "properties": { "provisioningState": "Succeeded" }, |
| 51 | + "type": "Microsoft.Resources/resourceGroups" |
| 52 | +} |
| 53 | +``` |
| 54 | +
|
| 55 | +### Create an action group |
| 56 | +
|
| 57 | +Create an action group to use as the notification target for the metric alert: |
| 58 | +
|
| 59 | +```bash |
| 60 | +az monitor action-group create \ |
| 61 | + --name my-ag \ |
| 62 | + --resource-group rg-alert-demo \ |
| 63 | + --short-name myag \ |
| 64 | + --action email admin admin@example.com |
| 65 | +``` |
| 66 | +
|
| 67 | +```bash title="Output" |
| 68 | +{ |
| 69 | + "groupShortName": "myag", |
| 70 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/microsoft.insights/actionGroups/my-ag", |
| 71 | + "name": "my-ag", |
| 72 | + "resourceGroup": "rg-alert-demo", |
| 73 | + "type": "Microsoft.Insights/ActionGroups" |
| 74 | +... |
| 75 | +} |
| 76 | +``` |
| 77 | +
|
| 78 | +### Create a metric alert |
| 79 | +
|
| 80 | +The following alert fires when CPU percentage on a virtual machine exceeds 80%: |
| 81 | +
|
| 82 | +```bash |
| 83 | +SCOPE="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Compute/virtualMachines/my-vm" |
| 84 | +AG_ID="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/microsoft.insights/actionGroups/my-ag" |
| 85 | + |
| 86 | +az monitor metrics alert create \ |
| 87 | + --name cpu-alert \ |
| 88 | + --resource-group rg-alert-demo \ |
| 89 | + --scopes "$SCOPE" \ |
| 90 | + --condition "avg Percentage CPU > 80" \ |
| 91 | + --action "$AG_ID" \ |
| 92 | + --description "Alert when CPU > 80%" |
| 93 | +``` |
| 94 | +
|
| 95 | +```bash title="Output" |
| 96 | +{ |
| 97 | + "criteria": { |
| 98 | + "allOf": [ |
| 99 | + { |
| 100 | + "criterionType": "StaticThresholdCriterion", |
| 101 | + "metricName": "Percentage CPU", |
| 102 | + "name": "cond0", |
| 103 | + "operator": "GreaterThan", |
| 104 | + "threshold": 80.0, |
| 105 | + "timeAggregation": "Average" |
| 106 | + } |
| 107 | + ], |
| 108 | + "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria" |
| 109 | + }, |
| 110 | + "description": "Alert when CPU > 80%", |
| 111 | + "enabled": true, |
| 112 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/metricAlerts/cpu-alert", |
| 113 | + "name": "cpu-alert", |
| 114 | + "resourceGroup": "rg-alert-demo", |
| 115 | + "severity": 2, |
| 116 | + "type": "Microsoft.Insights/metricAlerts", |
| 117 | + ... |
| 118 | +} |
| 119 | +``` |
| 120 | +
|
| 121 | +### Show and list metric alerts |
| 122 | +
|
| 123 | +Retrieve the details of the metric alert and list all alerts in the resource group: |
| 124 | +
|
| 125 | +```bash |
| 126 | +az monitor metrics alert show \ |
| 127 | + --name cpu-alert \ |
| 128 | + --resource-group rg-alert-demo |
| 129 | +``` |
| 130 | +
|
| 131 | +```bash title="Output" |
| 132 | +{ |
| 133 | + "criteria": { |
| 134 | + "allOf": [ |
| 135 | + { |
| 136 | + "metricName": "Percentage CPU", |
| 137 | + "operator": "GreaterThan", |
| 138 | + "threshold": 80.0, |
| 139 | + "timeAggregation": "Average", |
| 140 | + ... |
| 141 | + } |
| 142 | + ], |
| 143 | + "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria" |
| 144 | + }, |
| 145 | + "description": "Alert when CPU > 80%", |
| 146 | + "enabled": true, |
| 147 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/metricAlerts/cpu-alert", |
| 148 | + "name": "cpu-alert", |
| 149 | + "resourceGroup": "rg-alert-demo", |
| 150 | + "severity": 2, |
| 151 | + "type": "Microsoft.Insights/metricAlerts" |
| 152 | +... |
| 153 | +} |
| 154 | +``` |
| 155 | +
|
| 156 | +
|
| 157 | +Then list all metric alerts in the resource group: |
| 158 | +
|
| 159 | +```bash |
| 160 | +az monitor metrics alert list \ |
| 161 | + --resource-group rg-alert-demo |
| 162 | +``` |
| 163 | +
|
| 164 | +```bash title="Output" |
| 165 | +[ |
| 166 | + { |
| 167 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/metricAlerts/cpu-alert", |
| 168 | + "name": "cpu-alert", |
| 169 | + "resourceGroup": "rg-alert-demo", |
| 170 | + "severity": 2, |
| 171 | + "type": "Microsoft.Insights/metricAlerts" |
| 172 | + } |
| 173 | +] |
| 174 | +``` |
| 175 | +
|
| 176 | +### Create a service health activity log alert |
| 177 | +
|
| 178 | +Create an [activity log alert for Service Health](https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-activity-log) events. The condition `category=ServiceHealth` matches the default pattern documented for [`az monitor activity-log alert create`](https://learn.microsoft.com/en-us/cli/azure/monitor/activity-log/alert#az-monitor-activity-log-alert-create). That is different from alerting on administrative operations such as deleting a virtual machine, which uses other activity log fields (for example `category` and `operationName` for the delete operation). |
| 179 | +
|
| 180 | +```bash |
| 181 | +az monitor activity-log alert create \ |
| 182 | + --name service-health-alert \ |
| 183 | + --resource-group rg-alert-demo \ |
| 184 | + --scope "/subscriptions/00000000-0000-0000-0000-000000000000" \ |
| 185 | + --condition category=ServiceHealth \ |
| 186 | + --action-group "$AG_ID" |
| 187 | +``` |
| 188 | +
|
| 189 | +```bash title="Output" |
| 190 | +{ |
| 191 | + "actions": { |
| 192 | + "actionGroups": [ |
| 193 | + { |
| 194 | + "actionGroupId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/microsoft.insights/actionGroups/my-ag" |
| 195 | + } |
| 196 | + ] |
| 197 | + }, |
| 198 | + "condition": { |
| 199 | + "allOf": [ { "equals": "ServiceHealth", "field": "category" } ] |
| 200 | + }, |
| 201 | + "enabled": true, |
| 202 | + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-alert-demo/providers/Microsoft.Insights/activityLogAlerts/service-health-alert", |
| 203 | + "name": "service-health-alert", |
| 204 | + "resourceGroup": "rg-alert-demo", |
| 205 | + "scopes": ["/subscriptions/00000000-0000-0000-0000-000000000000"], |
| 206 | + "type": "Microsoft.Insights/ActivityLogAlerts" |
| 207 | +... |
| 208 | +} |
| 209 | +``` |
| 210 | +
|
| 211 | +### Delete and verify |
| 212 | +
|
| 213 | +Delete the metric alert rule and confirm it no longer appears in the metric alert list: |
| 214 | +
|
| 215 | +```bash |
| 216 | +az monitor metrics alert delete \ |
| 217 | + --name cpu-alert \ |
| 218 | + --resource-group rg-alert-demo |
| 219 | +``` |
| 220 | +
|
| 221 | +
|
| 222 | +Then list metric alerts in the resource group to confirm the rule was removed: |
| 223 | +
|
| 224 | +```bash |
| 225 | +az monitor metrics alert list --resource-group rg-alert-demo |
| 226 | +``` |
| 227 | +
|
| 228 | +```bash title="Output" |
| 229 | +[] |
| 230 | +``` |
| 231 | +
|
| 232 | +## Features |
| 233 | +
|
| 234 | +- **Metric alert lifecycle:** Create, read, list, update, and delete metric alert rules. |
| 235 | +- **Activity log alert lifecycle:** Create, read, list, and delete activity log alert rules. |
| 236 | +- **Single and multi-resource scopes:** Define alerts scoped to a single resource or multiple resources. |
| 237 | +- **Action group references:** Associate action groups with alert rules. |
| 238 | +- **Dynamic threshold support:** Accept dynamic threshold criteria in the alert condition. |
| 239 | +- **Severity configuration:** Set alert severity from 0 (Critical) to 4 (Verbose). |
| 240 | +- **Auto-mitigation:** Configure whether alerts auto-resolve when the condition clears. |
| 241 | +- **Frequency and window size:** Configure evaluation frequency and aggregation window size. |
| 242 | +
|
| 243 | +## Limitations |
| 244 | +
|
| 245 | +- **No alert evaluation:** Metric conditions are not evaluated against real or simulated metric data. Alerts never fire. |
| 246 | +- **No state transitions:** Alert state (Fired, Resolved) is not tracked or updated. |
| 247 | +- **No email or webhook dispatch:** Even if an alert were to fire, no notifications would be sent. |
| 248 | +- **Metrics not ingested:** Metric data is not ingested, stored, or queryable from LocalStack. |
| 249 | +
|
| 250 | +## Samples |
| 251 | +
|
| 252 | +Explore end-to-end examples in the [LocalStack for Azure Samples](https://github.com/localstack/localstack-azure-samples) repository. |
| 253 | +
|
| 254 | +## API Coverage |
| 255 | +
|
| 256 | +<AzureFeatureCoverage service="Microsoft.Insights" client:load /> |
0 commit comments