I'm using the terraform template_file
data resource to create a file that should be written to the dynamically-created EC2 instance when I apply the stack. In other words, I want this file to be created in the home folder of the newly-created EC2 instance. However, this file contains curly bracket syntax ${}
, which terraform is trying to interpolate. How can I escape these curly brackets?
As background, I'm using the cloud-config
syntax to write these files.
Ex:
${username}
should be written to the file, not interpolated in terraform.
Even when I use the double dollar sign $$
, terraform still fails because it can't find the variable:
... failed to render : <template_file>:105,18-26: Unknown variable; There is no variable named "username".
Terraform uses a fairly unique escape for curly braces:
Sequence | Replacement |
---|---|
$${ | Literal ${, without beginning an interpolation sequence. |
%%{ | Literal %{, without beginning a template directive sequence. |
Documentation for reference: https://www.terraform.io/docs/language/expressions/strings.html
FYI I ended up working around this by writing the template in another file, then reading it into the terraform stack using the file
method:
data "template_file" "config" {
template = "${file("./user_data.tpl")}"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With