gpt4 book ai didi

function - Terraform - 迭代模板中的对象列表

转载 作者:行者123 更新时间:2023-12-03 23:26:26 28 4
gpt4 key购买 nike

我在迭代由 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>
}

最佳答案

我刚刚发现(在制作了一个小型 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。

关于function - Terraform - 迭代模板中的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64651270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com