0

我正在努力使用 Terraform 脚本在 azure 中创建警报。我正在尝试为不同的资源创建不同的指标类型警报。(例如:functionapp01、functionapp02、logicapp01和logicapp02等)

这是脚本:

terraform {
  required_version = ">=0.12"
}

resource "azurerm_monitor_metric_alert" "metric_alert" {
  name                = var.metric_alert_name
  resource_group_name = var.rg_name
  scopes              = [var.resource_id_01,var.resource_id_02]
  description         = var.metric_alert_description
  tags                = var.tags
  frequency           = var.frequency
  severity            = var.severity
  window_size         = var.window_size
  enabled             = var.is_enabled
  
  criteria {
    metric_namespace = var.metric_namespace
    metric_name      = var.metric_name
    aggregation      = var.aggregation
    operator         = var.operator
    threshold        = var.threshold
  }

  action {
    action_group_id = var.action_group_id
  }

}

每当我运行上述脚本时,我都会收到以下错误:

服务返回错误。Status=400 Code="BadRequest" Message="microsoft.web/sites 的多资源级别当前不支持警报

参考链接:

azurerm_monitor_metric_alert

支持的指标和维度

那么,任何人都可以在这个问题上建议我吗?

4

1 回答 1

1

如错误所示,microsoft.web/sites资源类型不支持多资源警报,请参阅https://docs.microsoft.com/en-us/azure/azure-monitor/platform/alerts-metric-near-real-time#支持度量和维度 在此处输入图像描述 在此处输入图像描述

在这种情况下,您必须在每个资源级别上创建每个 monitor_metric_alert。

例如,如果您创建了两个函数,functionapp01、functionapp02。你可以这样做。

variable "function_apps" {
  default = ["functionapp01","functionapp02"]
}


data "azurerm_function_app" "example" {
  for_each = toset(var.function_apps)
  name                = each.value
  resource_group_name = "funtions_rg"
}


resource "azurerm_monitor_metric_alert" "metric_alert" {
  for_each = toset(var.function_apps)
  name                = "${each.value}-example-metricalert"
  resource_group_name = var.rg_name
  scopes              = [data.azurerm_function_app.example[each.value].id]
  description         = var.metric_alert_description
  tags                = var.tags
  frequency           = var.frequency
  severity            = var.severity
  window_size         = var.window_size
  enabled             = var.is_enabled
  
  criteria {
    metric_namespace = var.metric_namespace
    metric_name      = var.metric_name
    aggregation      = var.aggregation
    operator         = var.operator
    threshold        = var.threshold
  }

  action {
    action_group_id = var.action_group_id
  }

}

更新

如果您正在使用 Terraform 创建函数应用资源,则可以像这样使用它们:

            variable "function_apps" {
              default = ["functionapp01","functionapp02"]
            }
            
         resource "azurerm_function_app" "example" {
            for_each = toset(var.function_apps)
            name                      = "${each.value}-example-funapp"
            location                  = azurerm_resource_group.example.location
            resource_group_name       = azurerm_resource_group.example.name
            app_service_plan_id       = azurerm_app_service_plan.example.id
          
        
            storage_account_name       = azurerm_storage_account.example.name
            storage_account_access_key = azurerm_storage_account.example.primary_access_key
          
            app_settings = { }
          
            version = "~3"
           
          }
        
        
        
        resource "azurerm_monitor_metric_alert" "test" {
          for_each = toset(var.function_apps)
          name                = "${each.value}-example-metricalert"
          resource_group_name = azurerm_resource_group.example.name
          scopes              = [azurerm_function_app.example[each.value].id]
          description         = var.metric_alert_description
          severity            = var.severity
          window_size         = var.window_size
          enabled             = var.is_enabled
      
            criteria {
        metric_namespace = var.metric_namespace
        metric_name      = var.metric_name
        aggregation      = var.aggregation
        operator         = var.operator
        threshold        = var.threshold
      }
    
        
  action {
    action_group_id = var.action_group_id
  }
}
于 2020-12-25T01:22:59.810 回答