I am trying to create an alert policy within GCP using Terraform and Based on a boolean value I want to choose wether to create condition_threshold or conditionAbsent type policy. Since these are both different kinds of blocks within google_monitoring_alert_policy, I am not able to find a way to create either one or other based on a boolean value.
resource "google_monitoring_alert_policy" "logging_alert_policy" {
for_each = local.metrics_map
project = var.project
display_name = lower(format("%s%s%s%s", join("_", [element(split("-", each.value.appcode), length(split("-", each.value.appcode)) - 1), element(split("-", each.value.appcode), 0)]), " ", "log_match ", each.value.display_name))
combiner = each.value.how_to_trigger
dynamic "conditions" {
for_each = each.value.conditions
content {
display_name = lower(format("%s%s%s%s", join("_", [element(split("-", conditions.value.appcode), length(split("-", conditions.value.appcode)) - 1), element(split("-", conditions.value.appcode), 0)]), " ", "log_match ", conditions.value.display_name))
# condition_threshold {
# filter = lower("metric.type=\"logging.googleapis.com/user/${format("%s%s%s%s", conditions.value.appcode, "-", lower(replace(lookup(conditions.value, "metric_name"), "/\\W|_|\\s/", "-")), "")}\" resource.type=\"${conditions.value.resource_type}\"")
# comparison = conditions.value.comparison
# threshold_value = conditions.value.threshold_value
# duration = conditions.value.duration
# trigger {
# count = lookup(conditions.value, "trigger_count", var.default_metric_alert["trigger_count"])
# }
# aggregations {
# alignment_period = conditions.value.alignment_period
# per_series_aligner = conditions.value.per_series_aligner
# cross_series_reducer = conditions.value.cross_series_reducer
# group_by_fields = conditions.value.group_by_fields
# }
# }
condition_absent {
aggregations {
alignment_period = "300s"
per_series_aligner = "ALIGN_MAX"
cross_series_reducer = "REDUCE_MAX"
group_by_fields = ["resource.label.container_name"]
}
trigger {
count = 1
}
duration = "180s"
filter = lower("metric.type=\"logging.googleapis.com/user/${format("%s%s%s%s", conditions.value.appcode, "-", lower(replace(lookup(conditions.value, "metric_name"), "/\\W|_|\\s/", "-")), "")}\" resource.type=\"${conditions.value.resource_type}\"")
}
}
}
I would propose nested dynamic blocks that rely on local
computed set of one element - or no elements.
Short example of that idea:
locals {
absent = false # let's say we want to have "condition_threshold" block if this is true
conditions_config = local.absent == false ? ["1"] : []
absent_conditions_config = local.absent == true ? ["1"] : []
# I agree the above code is ugly but shows exactly the idea
}
# [...]
dynamic "conditions" {
for_each = each.value.conditions
content {
#[...]
dynamic "condition_threshold" {
for_each = toset(local.conditions_config) # this will be one element if local.absent = false
# content { ... }
}
dynamic "condition_absent" {
for_each = toset(local.absent_conditions_config) # this will be one element if local.absent = true
# content { ... }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.