Search code examples
terraformstring-interpolation

Terraform interpolation - convert string to list


How can I transform a string "foo, bar, xid, uid" to a list ["foo", "bar", "xid", "uid"]?

I'm assigninng to variable value from Consul which is a string

my_var = "foo, bar, xid, uid"

Now I would like to convert my_var to a list that will look like this:

my_list = ["foo", "bar", "xid", uid"]

How can I achieve that? I was trying to use formatlist and the splat operator for that but no success

my_list = ${formatlist(<put magic here>, var.my_var)

Solution

  • You can use the split function here:

    my_list = ${split(",", var.my_var)}