Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a list to a string in Terraform?

join works BUT i want to keep the double quotes join gives me this

[ben,linda,john]

BUT i want this

["ben", "linda", "john"]

this is getting crazy, spent over 2 hours trying to fix this i want to pass in a list as a string variable why can't terraform just take in my list as a string? why is this so difficult?

so i have

name = ["ben", "linda", "john"]

and i want to pass this to variable used in terrform

var.name

why can't terrform take this as is?

i get the error saying epxtected a string and i can not find a solution online after sarching everywhere

i have been able to get

[ ben,linda,john ] using join(",", var.name) but i want ["ben", "linda", "john"]

$ terraform --version
Terraform v0.12.18
+ provider.aws v2.42.0
+ provider.template v2.1.2
like image 365
uberrebu Avatar asked Dec 17 '19 20:12

uberrebu


People also ask

How do I turn a list into a string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

What is string interpolation in Terraform?

Embedded within strings in Terraform, whether you're using the Terraform syntax or JSON syntax, you can interpolate other values. These interpolations are wrapped in ${} , such as ${var. foo} . The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc.

Is Terraform list ordered?

The important thing here is that, as you've noticed, Terraform's map type is an unordered map which identifies elements only by their keys, not by permission. Therefore if you have a situation where you need to preserve the order of a sequence then a map is not a suitable data structure to use.


1 Answers

Conversion from list to string always requires an explicit decision about how the result will be formatted: which character (if any) will delimit the individual items, which delimiters (if any) will mark each item, which markers will be included at the start and end (if any) to explicitly mark the result as a list.

The syntax example you showed looks like JSON. If that is your goal then the easiest answer is to use jsonencode to convert the list directly to JSON syntax:

jsonencode(var.names)

This function produces compact JSON, so the result would be the following:

["ben","linda","john"]

Terraform provides a ready-to-use function for JSON because its a common need. If you need more control over the above decisions then you'd need to use more complex techniques to describe to Terraform what you need. For example, to produce a string where each input string is in quotes, the items are separated by commas, and the entire result is delimited by [ and ] markers, there are three steps:

  • Transform the list to add the quotes: [for s in var.names : format("%q", s)]
  • Join that result using , as the delimiter: join(", ", [for s in var.names : format("%q", s)])
  • Add the leading and trailing markers: "[ ${join(",", [for s in var.names : format("%q", s)])} ]"

The above makes the same decisions as the JSON encoding of a list, so there's no real reason to do exactly what I've shown above, but I'm showing the individual steps here as an example so that those who want to produce a different list serialization have a starting point to work from.

For example, if the spaces after the commas were important then you could adjust the first argument to join in the above to include a space:

"[ ${join(", ", [for s in var.names : format("%q", s)])} ]"
like image 171
Martin Atkins Avatar answered Oct 14 '22 17:10

Martin Atkins