You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform可复用模块中设置dimension块为可选

实现Terraform监控指标告警模块中dimension块的可选配置

要让azurerm_monitor_metric_alert资源的dimension块支持可选配置,只需通过动态块(dynamic block)结合可选变量实现,具体步骤如下:

1. 更新模块变量定义(variables.tf)

在alert_rules的对象结构中添加可选的dimension字段,明确其类型并标记为可选:

variable "actiongroupid" {
  type        = string
  description = "ID of the action group"
}

variable "resource_group_name" {
  type        = string
  description = "Name of the resource group"
}

variable "alert_scope" {
  type        = list(string)
  description = "List of resource IDs to monitor"
}

variable "alert_rules" {
  type = map(object({
    alertname        = string
    metric_namespace = string
    severity         = number
    metric_name      = string
    frequency        = string # 替换any为string,类型更明确
    windowsize       = string # 替换any为string,类型更明确
    aggregation      = string
    description      = string
    operator         = string
    threshold        = number
    # 定义可选的dimension配置
    dimension = optional(object({
      name     = string
      operator = string
      values   = list(string)
    }))
  }))
  description = "Map of metric alert rules to create"
}

2. 修改模块中的资源配置

使用Terraform的dynamic块,仅当dimension配置存在时才生成对应的块:

resource "azurerm_monitor_metric_alert" "main" {
  for_each = var.alert_rules

  name                = each.value.alertname
  resource_group_name = var.resource_group_name
  description         = each.value.description
  scopes              = var.alert_scope
  severity            = each.value.severity
  frequency           = each.value.frequency
  window_size         = each.value.windowsize

  criteria {
    metric_namespace = each.value.metric_namespace
    threshold        = each.value.threshold
    metric_name      = each.value.metric_name
    aggregation      = each.value.aggregation
    operator         = each.value.operator

    # 动态生成可选的dimension块
    dynamic "dimension" {
      for_each = each.value.dimension != null ? [each.value.dimension] : []
      content {
        name     = dimension.value.name
        operator = dimension.value.operator
        values   = dimension.value.values
      }
    }
  }

  action {
    action_group_id = var.actiongroupid
  }
}

3. 根模块调用示例

不带dimension的告警规则

直接省略dimension字段即可:

module "metric_alerts" {
  source               = "./modules/metric-alert"
  resource_group_name  = "your-resource-group"
  actiongroupid        = azurerm_monitor_action_group.email_alert.id
  alert_scope          = [azurerm_eventhub_namespace.test_eventhub.id]

  alert_rules = {
    "eventhub-no-dimension" = {
      alertname        = "eventhub-incoming-requests"
      metric_namespace = "Microsoft.EventHub/namespaces"
      severity         = 3
      metric_name      = "IncomingRequests"
      frequency        = "PT30M"
      windowsize       = "PT1H"
      aggregation      = "Total"
      description      = "触发条件:请求数大于90"
      operator         = "GreaterThan"
      threshold        = 90
    }
  }
}

带dimension的告警规则

添加dimension配置即可启用该块:

module "metric_alerts" {
  source               = "./modules/metric-alert"
  resource_group_name  = "your-resource-group"
  actiongroupid        = azurerm_monitor_action_group.email_alert.id
  alert_scope          = [azurerm_eventhub_namespace.test_eventhub.id]

  alert_rules = {
    "eventhub-with-dimension" = {
      alertname        = "eventhub-incoming-requests-specific"
      metric_namespace = "Microsoft.EventHub/namespaces"
      severity         = 3
      metric_name      = "IncomingRequests"
      frequency        = "PT30M"
      windowsize       = "PT1H"
      aggregation      = "Total"
      description      = "触发条件:指定EventHub的请求数大于90"
      operator         = "GreaterThan"
      threshold        = 90
      dimension = {
        name     = "EntityName"
        operator = "Include"
        values   = ["eventhubone"]
      }
    }
  }
}

内容的提问来源于stack exchange,提问作者Nikhil Babu Battula

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 15:00:47