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

uberrebu

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
Martin Atkins

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)])} ]"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert String[] to ArrayList<String>

Java: Can I convert List of Object to List of String[] and vice versa?

How do I convert a string to a list of chars?

How can I convert comma separated string into a List<int>

How can I convert string to dict or list?

How can I convert a string list to a list of ints?

How can I convert the list string to a float

How can I convert json map to List String?

How can I convert this random.choices python list to a string?

How can I convert a string into a list and then back to a string?

How can I convert a List<ExpandoObject> to an XML string

How can I convert a json string to a bean list?

How can I convert a comma delimited string to a list of integers?

How can I convert a tar encoded file into a terraform variable?

How can I convert a list to string

How can I convert 'System.Collection.Generic.List<String>' into 'System.Windows.Documents.List'?

How can I use recursion to convert a string to list of characters in Python?

How can I convert a String into a Char list?

How can I convert a string to a url string?

How I convert string response into Array List

terraform convert list to string dynamically

How can I convert string to the list as letter and letter in python?

Is there a way I can convert a list of string into a list of int in python/flask?

How can I convert a string to a list in a list? (Python)

How can I convert a 2d list from string into a python list?

Terraform — how can I properly do a string interpolation in this code?

How can I convert list of string to pandas DataFrame in Python

How can I convert a List<String> to String []?

how can i convert a string to a list to index using a for loop in lua?