user3264341
user3264341

Reputation: 117

Can I escape curly bracket symbol in 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".

Upvotes: 4

Views: 8450

Answers (3)

Hofo
Hofo

Reputation: 1

For example if you would like to add into Tags {{_payload.ref}} as it was in my case, I have done it this way:

tags                    = ["CRITICAL", "\u007B\u007B_payload.ref\u007D\u007D"]

so

\uNNNN  Unicode character from the basic multilingual plane (NNNN is four hex digits)

ref.page: https://www.terraform.io/language/expressions/strings

Upvotes: 0

Jack Casey
Jack Casey

Reputation: 1856

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

Upvotes: 8

user3264341
user3264341

Reputation: 117

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")}"
}

Upvotes: 2

Related Questions