Terraform templatefile function error when running with array in template

2.1k Views Asked by At

I have the following code:

terraform {
  required_version = ">= 0.13.1"
}

resource "local_file" "plan" {
  content  = templatefile("plan-pipeline-template.tftpl", var.generatedvars)
  filename = "${path.module}/plan-generated"
}

my plan-pipeline-template.tftpl file is:

%{ for var in ${VAR_PATHS} ~}
${var}-plan:
%{ endfor ~}

in order to provide the generatedvars which is being used by main.tf, i run the following:

export TF_VAR_generatedvars='{VAR_PATHS=["aaa","vvv"]}'

but it doesn't work, and i get:

Error: Error in function call

  on main.tf line 6, in resource "local_file" "plan":
   6:   content  = templatefile("plan-pipeline-template.tftpl", var.generatedvars)
    |----------------
    | var.generatedvars is map of tuple with 1 element

Call to function "templatefile" failed:
plan-pipeline-template.tftpl:1,15-16:
Invalid character; This character is not used within the language., and 1
other diagnostic(s).

if i change my plan-pipeline-template.tftpl with a hardcoded array, it works:

%{ for var in ["jj","kk"] ~}
${var}-plan:
%{ endfor ~}

which generates

jj-plan
kk-plan

any idea what am i doing wrong?

1

There are 1 best solutions below

1
On

The problem is in this line:

%{ for var in ${VAR_PATHS} ~}

seems like i don't need to use ${VAR_PATHS} instead, i should just use VAR_PATHS, because it is already considered as a variable inside a %{for...} loop.

This should work:

%{ for var in VAR_PATHS ~}