我在地面上设置了几个警报,还有几个有相同的政策。我能把它提取成某种变量吗?这样我就不必把它硬编码到每个警报中了吗?警报器除了那个普通的情况外,还会有其他的情况。
示例:
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吗?
发布于 2022-01-07 07:23:04
您可以使用本地值和嵌套动态块的组合来实现这一点,但老实说,您最终会得到更多的代码,而不仅仅是在您拥有这些值的情况下输入它们。
另一种选择是使用Terraform模板,但是它并不是那么清晰或声明性的。
另一种选择是使用模块进行抽象,并在模块文档中提供与抽象一起提供的公共条件列表。
然后,该模块处理可能需要的所有混乱的嵌套动态块和循环。
在编写模块之后,在本例中调用/配置代码可能如下所示:
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"
]
}https://stackoverflow.com/questions/70614811
复制相似问题