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

创建Azure Monitor诊断设置遇each.value引用问题求助

解决Azure Monitor活动日志警报Terraform配置中的each.value引用问题

问题根源

  1. 属性路径错误:变量中service_health、resource_health等属性嵌套在criteria对象下,但原代码直接引用each.value.service_health,路径不匹配
  2. 属性缺失报错:不同类型的警报规则(Administrative/ServiceHealth/ResourceHealth)包含的属性不一致,硬编码所有子块会导致部分规则因属性缺失失败
  3. 数组属性语法错误:原代码直接将数组赋值给statuses等嵌套块,不符合Terraform的嵌套块语法要求

修复后的配置

修正变量名冲突(可选但建议)

在variables.tf中,resourcehealth的名称与servicehealth重复,修改为唯一名称:

variable "activity_log" {
  description = "Azure monitor activity log alert rules for postgres server"
  type        = any

  default = {
    # ... 其他规则保持不变 ...

    resourcehealth = {
      name        = "psqlsrv_resourcehealth" # 修正重复名称
      description = "Alert rule if this server has a resource health alerts"

      criteria = {
        category = "ResourceHealth"
        resource_health = {
          current  = ["Degraded", "Unavailable", "Unknown"]
          previous = ["Available"]
          reason   = ["PlatformInitiated", "UserInitiated", "Unknown"]
        }
      }
    }
  }
}

修正main.tf中的each.value引用及块渲染逻辑

resource "azurerm_monitor_activity_log_alert" "alert_rule" {
  for_each = var.activity_log

  name                = each.value.name
  description         = each.value.description
  scopes              = [azurerm_postgresql_flexible_server.postgres_server.id]
  resource_group_name = element(var.rgname, 1)

  criteria {
    category = each.value.criteria.category

    # 仅当规则包含operation_name时生成该块
    dynamic "operation_name" {
      for_each = lookup(each.value.criteria, "operation_name", null) != null ? [true] : []
      content {
        value = each.value.criteria.operation_name
      }
    }

    # 遍历statuses数组生成嵌套块
    dynamic "statuses" {
      for_each = lookup(each.value.criteria, "statuses", [])
      content {
        value = statuses.value
      }
    }

    # 仅当ServiceHealth类型规则时生成service_health块
    dynamic "service_health" {
      for_each = lookup(each.value.criteria, "events", null) != null ? [true] : []
      content {
        dynamic "events" {
          for_each = each.value.criteria.events
          content {
            value = events.value
          }
        }
      }
    }

    # 仅当ResourceHealth类型规则时生成resource_health块
    dynamic "resource_health" {
      for_each = lookup(each.value.criteria, "resource_health", null) != null ? [true] : []
      content {
        dynamic "current" {
          for_each = each.value.criteria.resource_health.current
          content {
            value = current.value
          }
        }

        dynamic "previous" {
          for_each = each.value.criteria.resource_health.previous
          content {
            value = previous.value
          }
        }

        dynamic "reason" {
          for_each = each.value.criteria.resource_health.reason
          content {
            value = reason.value
          }
        }
      }
    }
  }

  action {
    action_group_id = data.azurerm_monitor_action_group.action_group.id
  }

  tags = local.project_tags
}

关键修改说明

  • 修正属性路径:所有嵌套属性均从each.value.criteria下读取,匹配变量结构
  • 动态块渲染:使用dynamic块实现条件渲染,仅当对应属性存在时才生成子块,避免缺失属性报错
  • 数组处理:对statuses、events等数组类型属性,通过dynamic块遍历生成嵌套子块,符合Terraform语法要求
  • 类型适配:针对不同类型的警报规则自动适配所需属性块,无需硬编码所有可能的子块

内容的提问来源于stack exchange,提问作者user23350878

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 01:45:05