Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I escape curly bracket symbol in terraform?

Tags:

terraform

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".

like image 241
user3264341 Avatar asked Sep 05 '25 17:09

user3264341


2 Answers

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

like image 128
Jack Casey Avatar answered Sep 07 '25 18:09

Jack Casey


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")}"
}
like image 29
user3264341 Avatar answered Sep 07 '25 16:09

user3264341