2

I am upgrading our code from Terraform 0.11 to 0.12.29.

my older code that works on TF 0.11

my.tf:

data "templ_file" "dep" {
    template = "$${input}"

    vars {
        input = "${join(",", var.abc)}"
    }
}

Where abc is defined as:

variable "abc" {
    default = []
    type = list
}

Updated my.tf to following for TF 0.12.29:

...
vars = {
    input = join(",", var.abc)
}

But I am getting this error:

Error: Invalid function argument

  on ../modules/x/y/my.tf line 6, in data "templ_file" "dep":
   6:         input = join(",", var.abc)
    |----------------
    | var.abc is list of list of dynamic with 1 element

Invalid value for "lists" parameter: incorrect list element type: string
required.

I also saw this post: https://github.com/hashicorp/terraform/issues/20705 which suggest to use concat or flatten but I could not make it work. I am new to terraform so this might be simple question but I am unable to get this working.

Learner
  • 1,503
  • 6
  • 23
  • 44
  • 2
    What is the actual value of your variable? – Marcin Nov 03 '20 at 07:20
  • 1
    It looks like you might have a nested list there. If you change your variable definition to have a type of `list(string)` to be stricter (`list` is the equivalent of `list(any)`) you should get a clearer error message. This normally comes about because HCL1 in Terraform 0.11 and below allowed you to wrap lists in square brackets and still come out with a normal list instead of a nested list. This isn't the case in HCL2 and Terraform 0.12+ and you need to be more aware of your types in general. – ydaetskcoR Nov 03 '20 at 08:38
  • Thanks @Marcin @ydaetskcoR, Changing the definition to `list(string)` seem to have resolved the issue as I don't see that error msg now. However, I am not very clear as to why it worked, could you please explain? – Learner Nov 04 '20 at 01:24
  • Based on the error message, I think you hit a bug in Terraform 0.12 here where the `join` function wasn't correctly handling the case where the element type of the list was unknown ("dynamic", in this error message). Empty brackets `[]` don't include an example element to decide the element type, so Terraform relies on the type constraint to know what you intended. `list(string)` therefore makes it work even when it's empty. With all of that said, I think this has been addressed in later versions of Terraform so that an empty list of unknown type is accepted as a list of strings. – Martin Atkins Nov 06 '20 at 01:35
  • Explicitly stating what element type you intend is a good practice anyway, to make it clearer how to use a particular variable, so I'd suggest keeping the `list(string)` even if `list` _does_ work in later Terraform versions. – Martin Atkins Nov 06 '20 at 01:36
  • @MartinAtkins Thanks for the explanation, that helps. – Learner Nov 06 '20 at 02:09

0 Answers0