我在迭代由 解释的模板内的对象列表时遇到问题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>
}