Search code examples
terraform

Invalid characters in heredoc anchor


I am trying to use a multiline string in the provisioner "remote-exec" block of my terraform script. Yet whenever I use the EOT syntax as outlined in the documentation and various examples I get an error that complains about having: invalid characters in heredoc anchor.

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

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

    << EOT 
    echo \
    hi 
    EOT,
  ]
}

Update: Here is the working solution, read carefully if you are having this issue 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 you use in a provisioner "remote-exec" block must be inside the EOF. You cannot have both EOF and non EOF its one or the other.

The first line of EOF must begin like this, and you cannot have any whitespace in this line after <<EOF or else it will complain about having invalid characters in heredoc anchor:

  inline = [<<EOF

Your EOF must then end like this with the EOF at the same indentation as the ]

  EOF
  ]

Solution

  • Heredocs in Terraform are particularly funny about the surrounding whitespace.

    Changing your example to the following seems to get rid of the heredoc specific errors:

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

    You shouldn't need multiple heredocs in here at all though as the inline array is a list of commands that should be ran on the remote host. Using a heredoc with commands across multiple lines should work fine for you:

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