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

能否通过Terraform配置GCP Cloud SQL的通知偏好?求对应模块

使用Terraform配置GCP Cloud SQL通知偏好设置

完全可以通过Terraform配置GCP Cloud SQL的通知偏好,核心是配置通知渠道并关联对应的Cloud SQL告警策略来实现。你之前告警模块未生效,大概率是没正确关联通知渠道,或是告警规则的资源匹配条件写错了。

1. 配置通知渠道(以邮件为例)

先创建一个接收告警的通知渠道,这里用邮箱作为示例:

resource "google_monitoring_notification_channel" "sql_alert_channel" {
  display_name = "Cloud SQL 告警通知渠道"
  type         = "email"
  labels = {
    # 替换成你实际接收告警的邮箱
    email_address = "alert-receiver@your-domain.com"
  }

  enabled = true # 启用该通知渠道
}

2. 配置Cloud SQL告警策略并关联通知渠道

接下来创建针对Cloud SQL实例的告警规则,把它和上面的通知渠道绑定:

resource "google_monitoring_alert_policy" "sql_high_cpu_alert" {
  display_name = "Cloud SQL CPU使用率过高告警"
  combiner     = "OR"

  conditions {
    display_name = "CPU使用率持续超过80%"
    condition_threshold {
      # 过滤条件:指定监控的Cloud SQL实例,替换成你的项目ID和实例名
      filter = "metric.type=\"cloudsql.googleapis.com/database/cpu/utilization\" resource.type=\"cloudsql_database_instance\" resource.labels.database_id=\"你的GCP项目ID:CloudSQL实例名\""
      
      aggregations {
        alignment_period   = "60s"        # 每60秒聚合一次数据
        per_series_aligner = "ALIGN_MEAN" # 取平均值
      }

      threshold_value = 0.8 # 告警阈值:CPU使用率80%
      duration        = "60s" # 持续超过阈值60秒才触发告警
      comparison      = "COMPARISON_GT" # 大于阈值时触发
    }
  }

  # 关联之前创建的通知渠道
  notification_channels = [google_monitoring_notification_channel.sql_alert_channel.id]

  severity = "WARNING" # 告警级别,可选CRITICAL/WARNING/INFO等
}

关键注意事项

  • 务必替换代码中的占位信息:邮箱、GCP项目ID、Cloud SQL实例名称
  • 可以根据需求修改监控指标,比如内存使用率(cloudsql.googleapis.com/database/memory/utilization)、存储空间使用率(cloudsql.googleapis.com/database/disk/usage)等
  • 如果之前告警没生效,检查filter里的resource.labels.database_id是否和你的实例完全匹配,以及notification_channels是否正确引用了通知渠道的ID

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 21:20:32