Skip to content

azurermMonitorMetricAlert

Manages a Metric Alert within Azure Monitor.

Example Usage

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as azurerm from "./.gen/providers/azurerm";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: azurerm.
For a more precise conversion please use the --provider flag in convert.*/
const azurermResourceGroupExample = new azurerm.resourceGroup.ResourceGroup(
  this,
  "example",
  {
    location: "West Europe",
    name: "example-resources",
  }
);
const azurermStorageAccountToMonitor =
  new azurerm.storageAccount.StorageAccount(this, "to_monitor", {
    account_replication_type: "LRS",
    account_tier: "Standard",
    location: azurermResourceGroupExample.location,
    name: "examplestorageaccount",
    resource_group_name: azurermResourceGroupExample.name,
  });
const azurermMonitorActionGroupMain =
  new azurerm.monitorActionGroup.MonitorActionGroup(this, "main", {
    name: "example-actiongroup",
    resource_group_name: azurermResourceGroupExample.name,
    short_name: "exampleact",
    webhook_receiver: [
      {
        name: "callmyapi",
        service_uri: "http://example.com/alert",
      },
    ],
  });
const azurermMonitorMetricAlertExample =
  new azurerm.monitorMetricAlert.MonitorMetricAlert(this, "example_3", {
    action: [
      {
        action_group_id: azurermMonitorActionGroupMain.id,
      },
    ],
    criteria: [
      {
        aggregation: "Total",
        dimension: [
          {
            name: "ApiName",
            operator: "Include",
            values: ["*"],
          },
        ],
        metric_name: "Transactions",
        metric_namespace: "Microsoft.Storage/storageAccounts",
        operator: "GreaterThan",
        threshold: 50,
      },
    ],
    description:
      "Action will be triggered when Transactions count is greater than 50.",
    name: "example-metricalert",
    resource_group_name: azurermResourceGroupExample.name,
    scopes: [azurermStorageAccountToMonitor.id],
  });
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermMonitorMetricAlertExample.overrideLogicalId("example");

Argument Reference

The following arguments are supported:

  • name - (Required) The name of the Metric Alert. Changing this forces a new resource to be created.
  • resourceGroupName - (Required) The name of the resource group in which to create the Metric Alert instance. Changing this forces a new resource to be created.
  • scopes - (Required) A set of strings of resource IDs at which the metric criteria should be applied.
  • criteria - (Optional) One or more (static) criteria blocks as defined below.

-> NOTE One of either criteria, dynamicCriteria or applicationInsightsWebTestLocationAvailabilityCriteria must be specified.

  • dynamicCriteria - (Optional) A dynamicCriteria block as defined below.

-> NOTE One of either criteria, dynamicCriteria or applicationInsightsWebTestLocationAvailabilityCriteria must be specified.

  • applicationInsightsWebTestLocationAvailabilityCriteria - (Optional) A applicationInsightsWebTestLocationAvailabilityCriteria block as defined below.

-> NOTE One of either criteria, dynamicCriteria or applicationInsightsWebTestLocationAvailabilityCriteria must be specified.

  • action - (Optional) One or more action blocks as defined below.
  • enabled - (Optional) Should this Metric Alert be enabled? Defaults to true.
  • autoMitigate - (Optional) Should the alerts in this Metric Alert be auto resolved? Defaults to true.
  • description - (Optional) The description of this Metric Alert.
  • frequency - (Optional) The evaluation frequency of this Metric Alert, represented in ISO 8601 duration format. Possible values are pt1M, pt5M, pt15M, pt30M and pt1H. Defaults to pt1M.
  • severity - (Optional) The severity of this Metric Alert. Possible values are 0, 1, 2, 3 and 4. Defaults to 3.
  • targetResourceType - (Optional) The resource type (e.g. microsoftCompute/virtualMachines) of the target resource.

-> This is Required when using a Subscription as scope, a Resource Group as scope or Multiple Scopes.

  • targetResourceLocation - (Optional) The location of the target resource.

-> This is Required when using a Subscription as scope, a Resource Group as scope or Multiple Scopes.

  • windowSize - (Optional) The period of time that is used to monitor alert activity, represented in ISO 8601 duration format. This value must be greater than frequency. Possible values are pt1M, pt5M, pt15M, pt30M, pt1H, pt6H, pt12H and p1D. Defaults to pt5M.
  • tags - (Optional) A mapping of tags to assign to the resource.

An action block supports the following:

  • actionGroupId - (Required) The ID of the Action Group can be sourced from the azurermMonitorActionGroup resource
  • webhookProperties - (Optional) The map of custom string properties to include with the post operation. These data are appended to the webhook payload.

A criteria block supports the following:

  • metricNamespace - (Required) One of the metric namespaces to be monitored.
  • metricName - (Required) One of the metric names to be monitored.
  • aggregation - (Required) The statistic that runs over the metric values. Possible values are average, count, minimum, maximum and total.
  • operator - (Required) The criteria operator. Possible values are equals, notEquals, greaterThan, greaterThanOrEqual, lessThan and lessThanOrEqual.
  • threshold - (Required) The criteria threshold value that activates the alert.
  • dimension - (Optional) One or more dimension blocks as defined below.
  • skipMetricValidation - (Optional) Skip the metric validation to allow creating an alert rule on a custom metric that isn't yet emitted? Defaults to false.

A dynamicCriteria block supports the following:

  • metricNamespace - (Required) One of the metric namespaces to be monitored.
  • metricName - (Required) One of the metric names to be monitored.
  • aggregation - (Required) The statistic that runs over the metric values. Possible values are average, count, minimum, maximum and total.
  • operator - (Required) The criteria operator. Possible values are lessThan, greaterThan and greaterOrLessThan.
  • alertSensitivity - (Required) The extent of deviation required to trigger an alert. Possible values are low, medium and high.
  • dimension - (Optional) One or more dimension blocks as defined below.
  • evaluationTotalCount - (Optional) The number of aggregated lookback points. The lookback time window is calculated based on the aggregation granularity (windowSize) and the selected number of aggregated points. Defaults to 4.
  • evaluationFailureCount - (Optional) The number of violations to trigger an alert. Should be smaller or equal to evaluationTotalCount. Defaults to 4.
  • ignoreDataBefore - (Optional) The ISO8601 date from which to start learning the metric historical data and calculate the dynamic thresholds.
  • skipMetricValidation - (Optional) Skip the metric validation to allow creating an alert rule on a custom metric that isn't yet emitted?

A applicationInsightsWebTestLocationAvailabilityCriteria block supports the following:

  • webTestId - (Required) The ID of the Application Insights Web Test.
  • componentId - (Required) The ID of the Application Insights Resource.
  • failedLocationCount - (Required) The number of failed locations.

A dimension block supports the following:

  • name - (Required) One of the dimension names.
  • operator - (Required) The dimension operator. Possible values are include, exclude and startsWith.
  • values - (Required) The list of dimension values.

Attributes Reference

The following attributes are exported:

  • id - The ID of the metric alert.

Timeouts

The timeouts block allows you to specify timeouts for certain actions:

  • create - (Defaults to 30 minutes) Used when creating the Metric Alert.
  • update - (Defaults to 30 minutes) Used when updating the Metric Alert.
  • read - (Defaults to 5 minutes) Used when retrieving the Metric Alert.
  • delete - (Defaults to 30 minutes) Used when deleting the Metric Alert.

Import

Metric Alerts can be imported using the resourceId, e.g.

terraform import azurerm_monitor_metric_alert.main /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.Insights/metricAlerts/example-metricalert