Search code examples
terraformheredoc

Can you use a templatefile as an output value in terraform without the EOF and EOT artifacts?


I'm building out my infrastructure in terraform and one of the outputs that I would like is the ssh config formatted for the newly created server.

I created a template file:

Host ${serverLabel}
    HostName ${hostname}
    User ${user}
    IdentityFile ${identityFile}

And used it in an output:

output "ssh-config" {
  description = "The new server's ssh config"
  value = templatefile("${path.module}/ssh-config.tpl", {
    serverLabel=aws_instance.minecraft_ec2.tags.Name
    hostname= aws_instance.minecraft_ec2.public_ip
    user= "ec2-user",
    identityFile = "~/.ssh/minecraft_key"
  })
}

Which works fine except for one little hiccup. I end up with some heredoc tags in the output:

actual output with heredoc tags shown

It's not the end of the world because I can just copy formatted text between the heredocs, but is there a way of using a template file as an output value without getting the tags??

Update

In reply to Mark B's question:

What happens if you run terraform output --raw?

Apparently when I output it with the --raw flag the heredoc tags go away

for some reason heredoc tags go away

Which feels weird, but reading the description for the output command I guess makes sense:

-raw - If specified, Terraform will convert the specified output value to a string and print that string directly to the output, without any special formatting. This can be convenient when working with shell scripts, but it only supports string, number, and boolean values. Use -json instead for processing complex data types.

So I guess the heredoc tags are considered "special formatting". Makes sense, thought it was not what I expected.


Solution

  • It appears this is how Terraform now outputs multiline values, similar to how regular values had quotes added to them a few versions ago. To get the values without quotes or heredoc tags wrapping them, use terraform output --raw.