TF_VAR struct incorrectly parsed as string

I have a go process that invokes Terraform using go exec, to process more than one Terraform project. Think of terragrunt. Let’s call it “runner”. Runner gets an initial set of input, and passes the same input to each project it processes. Some of the input might not be relevant to a particular project.

When passing in the input via terraform.tfvars.json for a particular project, it works great, but unwanted warnings are generated for input that goes unused.

To avoid that I am trying to pass in the input using TF_VAR_ environment variables instead.

When it comes to passing in a structure, I am getting errors in Terraform that it is unable to find keys in the map.

For example, the Runner invokes go exec with an environment variable such as

cmd.Env = append(cmd.Env, "TF_VAR_geo={region=\"us-central1\",zone=\"us-central1-c\",multizone=\"false\",location=\"us-central1-c\"}")

I put "bash", "-c", "echo $TF_VAR_geo" in place of the terraform executable and it prints


But Terraform produces this:

│ Error: Unsupported attribute
│   on line 12, in provider "google":
│   12:   region  = var.geo.region
│     ├────────────────
│     │ var.geo is "{zone=\"us-central1-c\",multizone=\"false\",location=\"us-central1-c\",region=\"us-central1\"}"
│ Can't access attributes on a primitive-typed value (string).

Why is terraform treating the value as a string instead of a map?

Hi @jimsnab,

Terraform decides how to use a string you provided on either the command line or in an environment variable based on the type constraint you declared for that variable.

If you declare this variable as expecting a map or object type then Terraform will know that it needs to parse the value as an HCL expression. Otherwise it assumes a raw string interpretation because that’s typically more convenient in the case of simple, primitive-typed input variables.

Makes sense. I’ve never needed to declare the variable as a particular type before, because I’ve never needed TF_VAR or -var switch until now. Thanks.