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

问题描述

在遍历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>
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)