在 terraform 中组合两个没有公共字段的列表,以便在 datadog 查询中使用

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

我有两个具有相同类型的不同变量声明为

type = list(object({
    somepropA : string,
    somepropB : list(string) 
}))

我的列表1看起来像这样:

 default = [
    {
      propA : "some val 1"
      propB : ["string1"]
    },
    {
      propA : "some val 2"
      propB : ["string2", "string3"]
    }
]

我的列表2看起来像这样:

default = [
    {
      propC : "some other val 1"
      propD : ["some other string 1"]
    },
    {
      propC : "some other val 2"
      propD : ["some other string 2"]
    },
    {
      propC : "some other val 3"
      propD : ["some other string 3"]
    }
]

我想构建一个看起来像这样的组合列表

default = [
{
  propA : "some val 1"
  propB : ["string1"]
  propC : "some other val 1"
  propD : ["some other string 1"]
},
{
  propA : "some val 1"
  propB : ["string1"]
  propC : "some other val 2"
  propD : ["some other string 2"]
},
.
.
.
{
  propA : "some val 2"
  propB : ["string2", "string3"]
  propC : "some other val 3"
  propD : ["some other string 3"]
}

]

我使用嵌套的 for 来构建这个列表

locals {
  monitor_configuration = distinct(flatten([
    for k in var.list1 : [
      for v in var.list2 : {
        propA           = k.propA
        propB           = k.propB
        propC           = v.propC
        propD           = v.propD
        window          = "last_12h" //newly introduced property to keep query window configurable
      }
    ]
  ]))
}

随后在此数据狗查询中使用

count   = var.condition == "some condition"? length(local.monitor_configuration) : 0
    
query   = "avg(${local.monitor_configuration[count.index]["window"]}):anomalies(sum:client.SomeKnownFunnel{condition:${var.condition} AND propDlist IN (${join(",", local.monitor_configuration[count.index]["propD"])}) AND propAlist IN (${join(",", local.monitor_configuration[count.index]["propA"])})}.as_count(), 'basic', 2, direction='both', interval=120, alert_window='last_30m', count_default_zero='true') >= 0.8"

但我怀疑变量构造没有正确发生。我从 terraform 得到的只是一个令人沮丧的查询无效错误。我从 terraform plan 中精心挑选了一些查询,它们正在正确验证,因此我怀疑列表中的某些值要么为空,要么未正确构建,这导致某些配置出错。有没有比嵌套更好的方法来构造这个合并列表?如果查询有问题,也欢迎反馈。

这些列表的结构是固定的并在其他配置中使用,因此对列表本身的修改是侵入性的,我会作为最后的手段。请注意,此处显示的 datadog 配置是一个片段,所有属性都在我的 datadog 监视器中使用,因此它们都是必需的。

terraform datadog
1个回答
0
投票

保持这个问题不变,因为我找不到很多关于如何进行 terraform 嵌套列表构建的示例。就我而言,问题最终是输入不匹配,而代码本身按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.