Terraform Map

Inputs

# variable.tf

variable "simplemapvariable"{
    type = map(string)
}
# variables.tfvars

  simplemapvariable = {
    AppName = "ERP Application",
    Environment = "prod"
    Description = "This shows how to create a terraform map(string) variable"
  }

1. Raw Output.

# main.tf

output "b_simplemapvariable_0" {
  value = var.simplemapvariable
}
b_simplemapvariable_0 = tomap({
  "AppName" = "ERP Application"
  "Description" = "This shows how to create a terraform map(string) variable"
  "Environment" = "prod"
})

2. Raw Output, select specific key.

# main.tf

output "b_simplemapvariable_1" {
  value = var.simplemapvariable["AppName"]
}
b_simplemapvariable_1 = "ERP Application"

3. Using with resource.

Note that when you use for_each an each object is available containing the key/value pair of the map which can be accessed using each.key or each.value respectively.

# main.tf

resource "null_resource" "simplemap" {
    for_each = var.simplemapvariable
    triggers = {
        key =  each.key
        value =   each.value
    }
}

output "b_simplemapvariable_2" {
  value = null_resource.simplemap
}

output "b_simplemapvariable_3" {
  value = null_resource.simplemap["AppName"].triggers.value
}
b_simplemapvariable_2 = {
  "AppName" = {
    "id" = "6396904784802596468"
    "triggers" = tomap({
      "key" = "AppName"
      "value" = "ERP Application"
    })
  }
  "Description" = {
    "id" = "1684536734664300226"
    "triggers" = tomap({
      "key" = "Description"
      "value" = "This shows how to create a terraform map(string) variable"
    })
  }
  "Environment" = {
    "id" = "4610946685690819320"
    "triggers" = tomap({
      "key" = "Environment"
      "value" = "prod"
    })
  }
}
b_simplemapvariable_3 = "AppName"