How to use map(string) as input variable in Nomad .hcl file?


I’m trying to use an input variable of type map(string) in my Nomad .hcl file and I’m getting an error that says, “Invalid legacy index syntax; When using the legacy index syntax, chaining two indexes together is not permitted.”

My variable looks like this:

variable "test" {
    type = map(string)
    default = {
        ex1 = "abc"
        ex2 = "def"
        ex3 = "ghi"
        ex4 = "jkl"

I’m referencing it in the .hcl file like this:

config {
    image = "myhub/myapp:${var.test["ex2"]}"

And I’m building the -var arguments in PowerShell like this:

$hclFile = "myjob.hcl"
$ex1val  = "mno"
$ex2val  = "pqr"
$ex3val  = ""
$ex4val  = ""
$nomadArgs = "-var=`"test={ex1=`"$($ex1Val)`", ex2=`"$($ex2val)`"}`", ex3=`"$($ex3val)`"}`", ex4=`"$($ex4val)`"}`""
$nomadCmd = "nomad job run $($nomadArgs) $($hclFile)"
Invoke-Expression $nomadCmd

Which outputs a string that looks like this:
-var="test={ex1="mno", ex2="pqr", ex3="", ex4=""}"

I also tried the above without “ex3” and “ex4”. I also tried using this for $nomadArgs:

$nomadArgs = "-var=`"test={ex1=`"$($ex1Val)`", ex2=`"$($ex2val)`"}`""

As well as this:

$nomadArgs = "-var=`"test={ex1=`"$($ex1Val)`"}`" -var=`"test=ex2=`"$($ex2val)`"}`""

The only place I have been able to find the error message above is in the HCL Parser code, so I’m not sure what legacy syntax I’m using or how to correct it. I have seen a number of non-HashiCorp sites/posts that are using something very similar to what I’m using for map(string) variables, so I’m not sure what is triggering that error message.

My version of Nomad is 1.6.1.