Terraform-遍历模板中的对象列表

时间:2020-11-02 18:32:10

标签: function terraform terraform0.12+

在遍历templatefile函数解释的模板中的对象列表时遇到问题。

我有以下变量:

variable "destinations" {
  description = "A list of EML Channel Destinations."

  type = list(object({
    id  = string
    url = string
  }))
}

它以templatefile的形式传递给destinations函数。模板相关的代码段是这样的:

Destinations:
  %{ for dest in destinations ~}
  - Id: ${dest.id}
    Settings:
      URL: ${dest.url}
  %{ endfor }

在规划Terraform时会出现以下错误:

Error: "template_body" contains an invalid YAML: yaml: line 26: did not find expected key

我尝试将模板代码切换为以下代码:

Destinations:
  %{ for id, url in destinations ~}
  - Id: ${id}
    Settings:
      URL: ${url}
  %{ endfor }

哪个给出了另一个错误:

Call to function "templatefile" failed:
../../local-tfmodules/eml/templates/eml.yaml.tmpl:25,20-23: Invalid template
interpolation value; Cannot include the given value in a string template:
string required., and 2 other diagnostic(s).

[!] something went wrong when creating the environment TF plan

我得到的印象是,我在这里对数据类型进行迭代是不正确的,但是我无法理解该怎么做,也根本找不到任何文档。

这是我如何调用此模块的简化示例:

module "eml" {
  source = "../../local-tfmodules/eml"

  name = "my_eml"

  destinations = [
    {
      id  = "6"
      url = "https://example.com"
    },
    {
      id  = "7"
      url = "https://example.net"
    }
  ]
<cut>
}

3 个答案:

答案 0 :(得分:1)

您不能将var.destinations作为到模板的地图列表传递。它必须是字符串列表/字符串集。

但是您可以执行以下操作:

templatefile("eml.yaml.tmpl", 
          { 
            ids =  [for v in var.destinations: v.id]
            urls =  [for v in var.destinations: v.url] 
          }
    )

eml.yaml.tmpl在哪里

Destinations:
  %{ for id, url in zipmap(ids, urls)  ~}
  - Id: ${id}
    Settings:
      URL: ${url}
  %{ endfor ~}

答案 1 :(得分:0)

我刚刚发现(在制作了一个小的Terraform模块以仅测试templatefile输出之后),原始配置确实起作用了(至少在TF v0.12.29中)。

给出的错误有点像红色鲱鱼-问题与模板内的缩进有关,例如代替:

Destinations:
  %{ for destination in destinations  ~}
  - Id: ${destination.id}
    Settings:
      URL: ${destination.url}
  %{ endfor ~}

应该是:

Destinations:
  %{~ for destination in destinations  ~}
  - Id: ${destination.id}
    Settings:
      URL: ${destination.url}
  %{~ endfor ~}

在Terraform指令的开头注意多余的波浪号(~)。这样可以使Yaml对齐方式正常工作(有些行缩进不正确,有些行空白)。此后,我问题中的原始代码将按我预期的方式工作并产生有效的Yaml。

答案 2 :(得分:0)

由于您打算生成YAML结果,因此我建议遵循the templatefile documentation中关于generating JSON or YAML from a template的建议。

使用the yamlencode function将确保结果始终是有效的YAML,而不必担心正确放置换行符或引用/转义可能包含特殊字符的字符串。

这样编写您的templatefile通话:

templatefile("${path.module}/templates/eml.yaml.tmpl", {
  destinations = var.destinations
})

然后,在eml.yaml.tmpl中,使整个模板成为调用yamlencode的结果,如下所示:

${yamlencode({
  Destinations = [
    for dest in destinations : {
      Id = dest.id
      Settings = {
        URL = dest.url
      }
    }
  ]
})

请注意,yamlencode的参数是Terraform expression syntax而不是YAML语法,因为在这种情况下,Terraform负责执行YAML编码,而您要做的就是为Terraform提供合适的值按照yamldecode文档中给出的从Terraform类型到YAML类型的映射进行编码。