简体   繁体   English

Terraform 在资源中创建一种或另一种块类型

[英]Terraform create either one block type or another within a resource

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.我正在尝试使用 Terraform 在 GCP 中创建警报策略,并且基于 boolean 值我想选择是创建 condition_threshold 还是 conditionAbsent 类型的策略。 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.由于这些都是 google_monitoring_alert_policy 中的不同类型的块,我无法找到一种方法来根据 boolean 值创建一个或另一个。

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.我会建议嵌套的动态块依赖于一个元素的local计算集 - 或者没有元素。

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 { ... } 
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform:如何将 output 从一种资源传递到另一种资源? - Terraform: How to pass output from one resource to another? 在 terraform 上创建参数化资源策略 - Create parameterized resource policy on terraform Terraform:如何提供使用一个资源块创建的资源的属性? - Terraform: How to supply attributes of resources which where created using one resource block? 使用 Terraform 创建 AWS 资源组 - Create an AWS Resource Group with Terraform 如果变量不是 null,则创建 Terraform 资源 - Create Terraform resource if variable is not null terraform 资源应该根据变量创建多个CloudWatch告警,计划中确认了但只部署了一个告警 - terraform resource should create multiple CloudWatch alarms based on variable, which is confirmed in the plan but only deploys one alarm 在 Terraform for Cloud Run 中创建动态秘密变量块 - Creating a dynamic secret variable block within Terraform for Cloud Run Terraform 使用 count 循环变量和 if 语句来创建资源 - Terraform using count for both looping a variable and if statement to create the resource 如何使用 for_each 引用 Terraform 中的另一个资源? - How to use for_each referencing to another resource in Terraform? 使用 Terraform 创建实例类型 Fargate 的 EKS 节点组 - Create EKS node group of instance type Fargate with Terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM