HCL: Using template sequences in labels

I recently discovered that HCL2 can be used as a library and started working on it. I cannot find a better place and I’m not sure whether I should use Github for questions, so I’m sorry in advance if it’s the wrong place.

I created a simple HCL2 file:

name = "Meme Service"
description = "To be deployed on ${conf.region} and backup to ${conf.backupRegion}"

test "standalone" "unit-${conf.xyz}" {
  script = "mvn clean install -T1C -am -amd -pl"

When I try to use ${conf.xyz} in the label value of a block, I get the following error:

Invalid string literal; Template sequences are not allowed in this string. To include a literal “$”, double it (as “$$”) to escape it.

It seems like an intended behavior but I wanted to learn why this is the case. Thanks!

Hi @mustafaakin,

HCL is designed to allow an application to analyze the block structure prior to evaluating any expressions. For example, in Terraform the presence of various top-level blocks decides which variables are available in the hcl.EvalContext for expression evaluation.

Evaluation of argument expressions is a separate step in HCL’s main workflow. The higher-level wrappers around the core HCL API like gohcl and hcldec blend the structural analysis and the expression evaluation together making them appear like a single step, but internally they are just performing those two steps one after the other and so the same constraints apply.