如何在 Heredoc Terraform 中创建变量以在 for 块中使用

How do I create variables within Heredoc Terraform to be used inside a for block

提问人:RCHV 提问时间:7/13/2023 更新时间:7/14/2023 访问量:55

问:

<<EOT
    %{ for ip in aws_instance.example.*.private_ip ~}
    %{ part0 = split(".", ${ip})[0] }
    server ${part0}
    %{ endfor ~}
EOT

有没有办法在 for 块中分配一个变量,该变量可以在块完成之前用于以后

添加

%{ part0 = split(".", ${ip})[0] }

在 for 块中显示“无效的模板控件关键字”

terraform heredoc

评论

1赞 Mark B 7/13/2023
你不需要一个内部的块。尝试:${}%{}%{ part0 = split(".", ip)[0] }
0赞 RCHV 7/13/2023
谢谢马克。不幸的是,这是行不通的。%{ part0 = split(“.”, ip)[0] ~} 显示 part0 不是有效的模板控件关键字

答:

0赞 Mark B 7/14/2023 #1

您正在尝试在模板中定义一个变量 ()。我认为您不能在 heredoc 模板中定义这样的变量。您还尝试在调用中添加额外的字符串插值 (),其中不应使用字符串插值。part0${}split()

请试试这个:

<<EOT
    %{ for ip in aws_instance.example.*.private_ip ~}
    server ${split(".", ip)[0]}
    %{ endfor ~}
EOT