Terraform: invalid characters in heredoc anchor

2019-04-24 18:33发布

问题:

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
  ]

回答1:

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
  ]
}


回答2:

The here-document end-delimiter has a comma (,) at the end. That's not allowed.

Try this instead:

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

I'm unaware of the syntax requirements of the file, but a here-document end-delimiter needs to match the word used at its start.

Also, usually (in shells), the delimiter needs to come first on the line (no whitespace in front).

In fact, the Terraform documentation says this:

Multiline strings can use shell-style "here doc" syntax, with the string starting with a marker like <<EOT and then the string ending with EOT on a line of its own. The lines of the string and the end marker must not be indented.



标签: terraform