How do I emit a $ literal in front of an interpolated value in Terraform?

Viewed 96

I have a Terraform string. It could be a var. or a local. or an output of some resource. I want to interpolate that string into another string with $ in front. Unfortunately,

"$${var.my_string}"

doesn't work because $${var.my_string} is the escape sequence that tells Terraform not to interpolate var.my_string.

How do I join $ onto the front of an interpolated value?

2 Answers

One way to do this is to interpolate two strings: the string "$" and the string you want:

"${"$"}${var.my_string}"

Additionally to @James' own answer, here are couple more ways to do it:

  1. Unicode character escape sequence (0024 is the code for $): "\u0024${var.my_string}"

  2. A strip marker in the interpolation: "$ ${~var.my_string}"

When a strip marker is present, any spaces adjacent to it in the corresponding string literal (if any) are removed before producing the final value.

Related