Luis
Luis

Reputation: 625

Terraform map to string value

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

Answers (2)

mlukasik
mlukasik

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

Marcin
Marcin

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

Related Questions