首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复GCP警报条件,地形块-如何将它们提取成一个常数?

重复GCP警报条件,地形块-如何将它们提取成一个常数?
EN

Stack Overflow用户
提问于 2022-01-06 23:21:19
回答 1查看 222关注 0票数 0

我在地面上设置了几个警报,还有几个有相同的政策。我能把它提取成某种变量吗?这样我就不必把它硬编码到每个警报中了吗?警报器除了那个普通的情况外,还会有其他的情况。

示例:

代码语言:javascript
复制
resource "google_monitoring_alert_policy" "alert_policy_1" {
  display_name = "My Alert Policy 1"
  combiner     = "OR"
  conditions {
    display_name = "test condition"
    condition_threshold {
      filter     = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
      duration   = "60s"
      comparison = "COMPARISON_GT"
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_RATE"
      }
    }
  }

conditions {
    display_name = "test condition 2"
    condition_threshold {
      filter     = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
      duration   = "60s"
      comparison = "COMPARISON_GT"
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_RATE"
      }
    }
  }

  user_labels = {
    foo = "bar"
  }
}

resource "google_monitoring_alert_policy" "alert_policy_2" {
  display_name = "My Alert Policy 2"
  combiner     = "OR"
  conditions {
    display_name = "test condition"
    condition_threshold {
      filter     = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
      duration   = "60s"
      comparison = "COMPARISON_GT"
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_RATE"
      }
    }
  }

  user_labels = {
    foo = "bar"
  }
}

我能避免在这里重复test condition吗?

EN

回答 1

Stack Overflow用户

发布于 2022-01-07 07:23:04

您可以使用本地值和嵌套动态块的组合来实现这一点,但老实说,您最终会得到更多的代码,而不仅仅是在您拥有这些值的情况下输入它们。

另一种选择是使用Terraform模板,但是它并不是那么清晰或声明性的。

另一种选择是使用模块进行抽象,并在模块文档中提供与抽象一起提供的公共条件列表。

然后,该模块处理可能需要的所有混乱的嵌套动态块和循环。

在编写模块之后,在本例中调用/配置代码可能如下所示:

代码语言:javascript
复制
module "alert_policy_1" {
  source = ../modules/common-alerts

  display_name = "My Alert Policy 1"
  combiner     = "OR"
  
  # use dynamic blocks within the module to handle nested values in for an array of custom conditions
  custom_conditions = [
    {
      display_name = "test condition"
      condition_threshold {
        filter     = "metric.type=\"some different and unique metric" AND resource.type=\"gce_instance\""
        duration   = "60s"
        comparison = "COMPARISON_GT"
        aggregations {
          alignment_period   = "60s"
          per_series_aligner = "ALIGN_RATE"
        }
      }
    }
  ]

  # the module then has a documented list of common policies to consume
  common_conditions = [
    "google_compute_write_bytes_count"
  ]

  user_labels = {
    foo = "bar"
  }
}

module "alert_policy_2" {
  source = ../modules/common-alerts

  display_name = "My Alert Policy 2"
  combiner     = "OR"
  common_conditions = [
    "google_compute_write_bytes_count"
  ]

  user_labels = {
    foo = "bar"
  }
}

# if OR is common and as are the user labels they could be provided by module variable defaults.

module "alert_policy_3" {
  source = ../modules/common-alerts

  display_name = "My Alert Policy 3"  
  common_conditions = [
    "google_compute_write_bytes_count"
  ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70614811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档