How do you parse a map variable to a string in a resource value with Terraform12?
I have this variable:
variable "tags" {
type = map
default = {
deployment_tool = "Terraform"
code = "123"
}
}
And want this: {deployment_tool=Terraform, code=123}
I've tried the following without success:
resource "aws_ssm_parameter" "myparamstore" {
***
value = {
for tag in var.tags:
join(",",value, join("=",tag.key,tag.values))
}
}
You can use terraform lookup() function to get value of an element from a map based on key. lookup() retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.
» tomap Function tomap converts its argument to a map value. Explicit type conversions are rarely necessary in Terraform because it will convert types automatically where required. Use the explicit type conversion functions only to normalize types returned in module outputs.
Replacing ":" with "=" is not a perfect solution, just consider a map with such a value: https://example.com
- it becomes https=//example.com
. That's not good.
So here is my solution:
environment_variables = join(",", [for key, value in var.environment_variables : "${key}=${value}"])
Your requested output is just malformed JSON string. So you can convert your variable to json using jsonencode
, and then remove "
and change :
into =
:
value = replace(replace(jsonencode(var.tags), "\"", ""), ":", "=")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With