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

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

Is there a way to get a variable assigned in the for block which can be used for later before the block completes

adding

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

within for block shows "invalid template control keyword"

1

There are 1 best solutions below

0
Mark B On

You are trying to define a variable (part0) inside the template. I don't think you can define variables like that in a heredoc template. You're also trying to add an extra string interpolation (${}) inside the split() call, where string interpolation should not be used.

Please try this:

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