You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform Heredoc的for块内定义可复用变量?语法错误求解

Terraform Heredoc模板变量定义错误解决方法

你遇到的「invalid template control keyword」错误,是因为在Terraform的模板控制块里,定义局部变量必须使用set关键字,不能直接用赋值符号=。

修正后的代码

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

关键说明

  • 控制块内引用循环变量ip时,不需要加${}包裹,直接写变量名即可。
  • 如果这个拆分后的结果只需要用一次,也可以直接内联计算,省去变量定义:
    <<EOT
        %{ for ip in aws_instance.example.*.private_ip ~}
        server ${split(".", ip)[0]}
        %{ endfor ~}
    EOT
    

内容的提问来源于stack exchange,提问作者RCHV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 13:06:57