【问题标题】:Is there a way to only create a resource if lookup returns a value?如果查找返回值,有没有办法只创建资源?
【发布时间】:2022-07-05 04:09:46
【问题描述】:

我试图仅在数据结构定义了值时才创建资源。

我的数据结构是这样的:

  network = {
    region1 = {
      range1   = "x.x.x.x/x"
      range2 = "x.x.x.x/x"
    },
    region2 = {
      range1   = "x.x.x.x/x"
    }
  }

我有一个资源块,我只想在range2 存在时创建它。我一直在尝试这样的事情:

count = lookup(local.network[var.region], "range2", null) =! null ? 1 : 0

但是,我无法让它工作,错误是the given object has no attribute range2

有没有办法实现这个结果,只有在定义range2 时才会创建资源?

【问题讨论】:

    标签: terraform


    【解决方案1】:

    您可以使用can 函数返回一个布尔类型值,具体取决于不会引发错误的配置代码。在这种情况下,我们将检查值访问器是否抛出错误(相当于值存在):

    # modern for_each
    for_each = can(local.network[var.region]["secondary"]) ? toset(["this"]) : []
    # older count
    count = can(local.network[var.region]["secondary"]) ? 1 : 0
    

    您对条件资源元参数的使用遵循一般设计模式,因此无需进行任何更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 2019-03-15
      • 1970-01-01
      相关资源
      最近更新 更多