Terraform 重用配置块

问题描述 投票:0回答:1

这里是 Terraform 菜鸟。我有一个 datadog 配置(无耻地从 terraform 网站复制):

resource "datadog_dashboard" "ordered_dashboard" {
  title        = "Ordered Layout Dashboard"
  description  = "Created using the Datadog provider in Terraform"
  layout_type  = "ordered"
  is_read_only = true

  widget { # SIMILAR GRAPH
    alert_graph_definition {
      alert_id  = "895605"
      viz_type  = "timeseries"
      title     = "Widget Title"
      live_span = "1h"
    }
  }

  widget { # SIMILAR GRAPH
    alert_graph_definition {
      alert_id  = "895605"
      viz_type  = "timeseries"
      title     = "Widget Title"
      live_span = "2h"
    }
  }

  widget {
    alert_value_definition {
      alert_id   = "895605"
      precision  = 3
      unit       = "b"
      text_align = "center"
      title      = "Widget Title"
    }
  }

  widget { # SIMILAR GRAPH
    alert_graph_definition {
      alert_id  = "895605"
      viz_type  = "timeseries"
      title     = "Widget Title"
      live_span = "1w"
    }
  }

  widget { # SIMILAR GRAPH
    alert_graph_definition {
      alert_id  = "895605"
      viz_type  = "timeseries"
      title     = "Widget Title"
      live_span = "2w"
    }
  }
}

如图所示,我们有 5 个图:图 1、2、4 和 5 非常相似,仅在 1 个属性上不同,而图 3 则非常不同。 TF 中是否有策略可以重用图 1,2 4 和 5 的所有配置?

我尝试过使用动态块,但是虽然动态块可以很好地生成图1和图2,但图3是如此不同,以至于我无法将其放入动态块中。

在实践中,图表要复杂得多,而且数量更多,因此我发现配置太难管理。

这是我尝试过的:

resource "datadog_dashboard" "ordered_dashboard" {
  title        = "Ordered Layout Dashboard"
  description  = "Created using the Datadog provider in Terraform"
  layout_type  = "ordered"
  is_read_only = true

  dynamic widget {
    for_each = ["1h", "2h"]
    iterator = live_span_iterator
    content {
      alert_graph_definition {
        alert_id  = "895605"
        viz_type  = "timeseries"
        title     = "Widget Title"
        live_span = live_span_iterator.value
      }
    }
  }

  widget {
    alert_value_definition {
      alert_id   = "895605"
      precision  = 3
      unit       = "b"
      text_align = "center"
      title      = "Widget Title"
    }
  }

  dynamic widget {
    for_each = ["1w", "2w"]
    iterator = live_span_iterator
    content {
      alert_graph_definition {
        alert_id  = "895605"
        viz_type  = "timeseries"
        title     = "Widget Title"
        live_span = live_span_iterator.value
      }
    }
  }
}

还有其他策略可以帮助我减少代码重复吗?如果可能的话,我希望保留图表的顺序。

terraform code-duplication datadog
1个回答
0
投票

为了使其更加干燥,您可以采用

alert_value_definition
参数之间相同的所有参数并将它们设置在locals中或作为变量默认值,然后在动态块中使用合并函数

合并功能有这样的特点:

如果多个给定映射或对象定义相同的键或属性,则参数序列中较靠后的映射或对象优先。

因此您可以将整个

alert_value_definition
设置为本地,然后覆盖动态块内的一些参数。

示例(我现在无法测试此代码,但我希望您能明白):

locals {
 alert_graph_definition_defaults {
        alert_id  = "895605"
        viz_type  = "timeseries"
        title     = "Widget Title"
 }
}

resource "datadog_dashboard" "ordered_dashboard" {
  title        = "Ordered Layout Dashboard"
  description  = "Created using the Datadog provider in Terraform"
  layout_type  = "ordered"
  is_read_only = true

  dynamic widget {
    for_each = ["1h", "2h"]
    iterator = live_span_iterator
    content {
      alert_graph_definition = merge(
        local.alert_graph_definition_defaults,
        { live_span = live_span_iterator.value }
      )
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.