Terraform: invalid characters in heredoc binding

I am trying to use a multi-line string in the provisioner "remote-exec" block of my terraform script. However, whenever I use the EOT syntax, as described in the documentation and various examples, I get an error message: invalid characters in heredoc anchor .

Here is an example of a simple provisioner "remote-exec" that received this error (both types of EOT get this error when trying separately):

 provisioner "remote-exec" { inline = [ << EOT echo hi EOT, << EOT echo \ hi EOT, ] } 

Update: Here is a working solution, read carefully if you have this problem, because terraform is very picky when it comes to EOF:

 provisioner "remote-exec" { inline = [<<EOF echo foo echo bar EOF ] } 

Note that if you want to use EOF, all the commands that you use in the provisioner "remote-exec" block must be inside EOF. You cannot have either EOF or non-EOF one or the other.

The first line of EOF should start the same way, and you cannot have spaces in this line after <<EOF , otherwise it will complain about the presence of invalid characters in heredoc anchor :

  inline = [<<EOF 

Then your EOF should end like this: EOF in the same indent as ]

  EOF ] 
+5
source share
2 answers

Terraform's heredocs are especially funny around surrounding spaces.

Changing your example to the following seems to eliminate the specific heredoc errors:

 provisioner "remote-exec" { inline = [<<EOF echo hi EOF, <<EOF echo \ hi EOF ] } 

You do not need to use multiple heredocs at all, although the built-in array is a list of commands that should be run on the remote host. Using heredoc with multi-line commands should work just fine for you:

 provisioner "remote-exec" { inline = [<<EOF echo foo echo bar EOF ] } 
+6
source

The final delimiter of the document here has a comma ( , ) at the end. This is not allowed.

Try this instead:

 provisioner "remote-exec" { inline = [ <<EOT echo hi EOT , <<EOT echo \ hi EOT , ] } 

I do not know the syntax requirements for the file, but the final delimiter of the document here must match the word used at the beginning.

In addition, usually (in shells) the separator should be the first on the line (without spaces in front).

In fact, the Terraform documentation says the following:

Multiline strings can use shell-style syntax "here doc". a line starting with a marker of type <<EOT , and then ending with a line with EOT on a separate line. The line and line end marker should not be indented.

+1
source

All Articles