Terraform Map of Map

Inputs

# variable.tf

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

  simplemapmapvariable ={
    "ERP" = {
      AppName = "ERP Application",
      Environment = "prod"
      Description = "This is the ERP Application"
    },
    "CRM" = {
      AppName = "CRM Application",
      Environment = "prod"
      Description = "This is the CRM Application"
    }
  }
  

1. Raw Output

# main.tf

output "c_simplemapvariable_1" {
  value = var.simplemapmapvariable
}
c_simplemapvariable_1 = tomap({
  "CRM" = tomap({
    "AppName" = "CRM Application"
    "Description" = "This is the CRM Application"
    "Environment" = "prod"
  })
  "ERP" = tomap({
    "AppName" = "ERP Application"
    "Description" = "This is the ERP Application"
    "Environment" = "prod"
  })
})

2. Raw Output, select specific keys.

# main.tf

output "c_simplemapvariable_2" {
  value = var.simplemapmapvariable["CRM"]["AppName"]
}
c_simplemapvariable_2 = "CRM Application"

3. Map output, iterating over list

# main.tf

output "c_simplemapvariable_3" {
  value = {
      for app, app_details in var.simplemapmapvariable : app => merge({
        for i, v in app_details : i => v
        }, {"ShortCode" = app})
  }
}
c_simplemapvariable_3 = {
  "CRM" = {
    "AppName" = "CRM Application"
    "Description" = "This is the CRM Application"
    "Environment" = "prod"
    "ShortCode" = "CRM"
  }
  "ERP" = {
    "AppName" = "ERP Application"
    "Description" = "This is the ERP Application"
    "Environment" = "prod"
    "ShortCode" = "ERP"
  }
}

4. Using with resource.

# main.tf

resource "null_resource" "simplemapmapvariable"{
  for_each =     {
      for k,v in var.simplemapmapvariable : k => v
  }
  triggers = {
    AppShortCode = each.key
    AppName = each.value.AppName
    Environment = each.value.Environment
    Description = each.value.Description
  }

}

output "c_simplemapvariable_4" {
  value = null_resource.simplemapmapvariable
}

c_simplemapvariable_4 = {
  "CRM" = {
    "id" = "7473626954338350905"
    "triggers" = tomap({
      "AppName" = "CRM Application"
      "AppShortCode" = "CRM"
      "Description" = "This is the CRM Application"
      "Environment" = "prod"
    })
  }
  "ERP" = {
    "id" = "1489457881142150577"
    "triggers" = tomap({
      "AppName" = "ERP Application"
      "AppShortCode" = "ERP"
      "Description" = "This is the ERP Application"
      "Environment" = "prod"
    })
  }
}