0
votes

While creating metric alert on storage account via terraform I am getting Error 400

I've gone through the documentation and corss-verified that the name I am using for alert creation is correct

resource "azurerm_metric_alertrule" "test" {
name                = "alerttestacc"
resource_group_name = "${azurerm_resource_group.main.name}"
location            = "${azurerm_resource_group.main.location}"

description = "An alert rule to watch the metric Used capacity"

enabled = true

resource_id = "${azurerm_storage_account.to_monitor.id}"
metric_name = "UsedCapacity"
operator    = "GreaterThan"
threshold   = 20
aggregation = "Total"
period      = "PT5M"

email_action {
    send_to_service_owners = false

    custom_emails = [
    "[email protected]",
    ]
}

webhook_action {
    service_uri = "https://example.com/some-url"

    properties = {
        severity        = "incredible"
        acceptance_test = "true"
    }
}

Expected: Alert should be created

Actual:

azurerm_metric_alertrule.test: insights.AlertRulesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="UnsupportedMetric" Message="The metric with namespace '' and name 'UsedCapacity' is not supported for this resource id

1
why are you so sure that metric exists? it surely doesnt make a lot of sense to me4c74356b41
Azure documentation says that the metrics exist. linkJitu
CHeck this linkJitu
On the second link there is a metric_name where a link of all metrics is given.Jitu
extensive list linkJitu

1 Answers

0
votes

You could use azurerm_monitor_metric_alert instead of azurerm_metric_alertrule to create a UsedCapacity metric alert for the storage account. It is possible due to the different experiences between classic alerts and new alerts in Azure monitoring. Read alerts overview.

This example works on my side.

resource "azurerm_resource_group" "main" {
  name     = "example-resources"
  location = "West US"
}

resource "azurerm_storage_account" "to_monitor" {
  name                     = "examplestorageaccount123"
  resource_group_name      = "${azurerm_resource_group.main.name}"
  location                 = "${azurerm_resource_group.main.location}"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_monitor_action_group" "main" {
  name                = "example-actiongroup"
  resource_group_name = "${azurerm_resource_group.main.name}"
  short_name          = "exampleact"

  webhook_receiver {
    name        = "callmyapi"
    service_uri = "http://example.com/alert"
  }
}

resource "azurerm_monitor_metric_alert" "test" {
  name                = "example-metricalert"
  resource_group_name = "${azurerm_resource_group.main.name}"
  scopes              = ["${azurerm_storage_account.to_monitor.id}"]
  description         = "Action will be triggered when the Used capacity is Greater than 777 bytes."

  criteria {
    metric_namespace = "Microsoft.Storage/storageAccounts"
    metric_name      = "UsedCapacity"
    aggregation      = "Total"
    operator         = "GreaterThan"
    threshold        = 777

  }

  action {
    action_group_id = "${azurerm_monitor_action_group.main.id}"
  }
}

enter image description here