Reputation: 625
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))
}
}
Upvotes: 14
Views: 21894
Reputation: 431
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}"])
Upvotes: 29
Reputation: 238071
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), "\"", ""), ":", "=")
Upvotes: 14