Interpolation not working with list

terraform plan -var 'users=["Aniket","Rahul"]'
get_user_list.tf

 output "get_user_list" {
  value = "All users: ${var.users}"
}
│ Error: Invalid template interpolation value
│
│   on get_user_list.tf line 2, in output "get_user_list":
│    2:   value = "All users: ${var.users}"
│     ├────────────────
│     │ var.users is list of string with 2 elements

Does terraform does not support list interpolation ?

Hi @aniketpant1,

The last line of the error message you left out gives a little more detail

│ Cannot include the given value in a string template: string required.

Terraform needs a string value to use within a string template, but var.users is a list which is a value of a different type. You can format the list into a string if you choose, using join for example:

"All users: ${join(", ", local.users)}"

Or if you expect the data to appear more structured, jsonencode may suit you better:

"All users: ${jsonencode(local.users)}"
1 Like

Thanks @jbardin , just to clear one doubt if we give list of string it will not work ?

For example : list.tf is for taking user input

variable "users" {
  type = list(string)
}

and get_user_list.tf is for output

output "get_user_list" {
  value = "All users: ${var.users}"
}

Below is the output

terraform plan -out main.tfplan
var.users
  Enter a value: ["Aniket","Virat"]


Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: Invalid template interpolation value
│
│   on get_user_list.tf line 2, in output "get_user_list":
│    2:   value = "All users: ${var.users}"
│     ├────────────────
│     │ var.users is list of string with 2 elements
│
│ Cannot include the given value in a string template: string required.

Because whatever i understand from your point is that terraform will not give the output of list of string when we want to print the values in a list without using any function.

A list of strings (list(string)) is still a distinct type from a plain string, and Terraform cannot format a list into a string without some specific instructions on how to format that list. In order for Terraform to write the interpolated data into a string, that data must also be a string.

1 Like