How to use built-in function when I use .tf.json template

Hi all,
I’m trying to use Json template. I have one variable named ‘master’ and in the resource section, I would like to use something like below to define the name. May I know how can I use the function like ‘format’ or ‘element’ in Json template? thanks!

Native terraform template:
name = format("%s-subnet", var.master)

Hi @Shuanglu,

In parts of the configuration language where arbitrary expressions would be expected, JSON syntax uses the expression mapping rules.

Notice in particular that Terraform interprets a JSON string as a template that can include interpolation sequences like ${} in order to generate values dynamically. In your case the template would consist only of a single interpolation directive containing your function call:

{
  "name": "${format(\"%s-subnet\", var.master)}"
}

Because this expression includes quote symbols, we need to escape them as \" so that they can be interpreted as part of the template rather than as a terminator for the JSON string they’re embedded in.

1 Like