Paweł Krupiński
Paweł Krupiński

Reputation: 1426

How to transform a map to map in Terraform

I have a map in Terraform, values of which I want to transform by prepending them with a string, resulting in another map.

variable "my_map" {
  type = map(string)
}


locals {
  my_new_map = [for key, value in var.my_map: { key = "prefix/${value}"}]
}

But local.my_new_map is a tuple instead of a map. What am I missing for the result to be a map?

Upvotes: 8

Views: 5884

Answers (1)

Benoit74B
Benoit74B

Reputation: 1855

You must use the map syntax with { and } :

variable "my_map" {
  type = map(string)
}


locals {
  my_new_map = {for key, value in var.my_map: key => "prefix/${value}"}
}

Upvotes: 16

Related Questions