通过Terraform创建Azure警报失败,错误代码为400

时间:2019-01-10 09:51:19

标签: azure terraform azure-automation alerts terraform-provider-azure

通过Terraform在存储帐户上创建指标警报时,出现错误400

我仔细阅读了文档,并核实我用于创建警报的名称正确

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 = [
    "xyz@gmail.com",
    ]
}

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

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

预期:应创建警报

实际:

  

azurerm_metric_alertrule.test:   Insights.AlertRulesClient#CreateOrUpdate:回应失败   要求:StatusCode = 400-原始错误:autorest / azure:服务   返回错误。 Status = 400代码=“ UnsupportedMetric”消息=“ The   具有名称空间“”和名称“ UsedCapacity”的指标不支持   此资源ID

1 个答案:

答案 0 :(得分:0)

您可以使用azurerm_monitor_metric_alert而非azurerm_metric_alertrule为存储帐户创建 UsedCapacity 指标警报。由于经典警报和Azure监视中的新警报之间的体验不同,因此可能。阅读警报overview

这个例子对我有用。

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