I’m running into a situation where i need terraform to replace this list with strings enclosed in single quotes like this. [‘test1’,‘test2’]. How would i go about replacing a double quote character with a single quote character? I’m using the replace method, but getting an error when trying to replace with a single quote character. I believe terraform does not support single quotes. Another thing i would like is to create a new local that holds the string value “‘test1’,‘test2’”, this is basically removing the square brackets.
locals {
appengineversions = ["test1","test2"]
appengine_instances_list = replace(local.appengine,'\"',"\'")
}
I’m not quite sure I understand what you are asking. The code
locals {
appengineversions = ["test1", "test2"]
}
creates a new local.appengineversions
variable, which is a list containing two values: test1
and test2
. Those two values don’t contain any quotes.
Could you explain more about what you are trying to achieve, or what the problem you are trying to solve is?
sure hey stuart,
I’m basically trying to replace the double quotes with single quotes. Ideally what i need to have a new local that is similar to the appengineversions local, but just uses single quotes and not double quotations. Basically the user input is a list of app engine versions enclosed in double quotes, by default terraform uses double quotes to enclose strings, and not single. I need to basically take that local and update it and remove the double quotes and replace them with single quotes.
locals {
appengineversions = ["test1", "test2"]
updatedlist = ['test1','test2']
}
I’m still not really understanding. test1
and test2
are strings which contain no type of quotes. Are you in reality wanting to use strings which do contain quotes (e.g. an"example'string
)?
Terraform uses HCL (or alternatively JSON) to format the code files it uses (*.tf files). You therefore need to follow the syntax for HCL. For a collection this is defined (from hcl/spec.md at main · hashicorp/hcl · GitHub) as:
"[" (
(Expression ("," Expression)* ","?)?
) "]";
And in our case we are talking about Template Expressions (hcl/spec.md at main · hashicorp/hcl · GitHub) which are defined as:
A quoted template expression is delimited by quote characters ( "
) and defines a template as a single-line expression with escape characters.
Including:
\" Literal quote mark, used to prevent interpretation as end of string
So if we wanted to add that extra value to our code it would be:
locals {
appengineversion = ["test1", "test2", "an\"example'string"]
}