Skip to main content

All Code

Root Module : main.tf

# **** MAP
variable "MAP_1" {
type = map(string)
default = {
keyA = "ValueA"
keyB = "ValueB"
keyC = "ValueC"
}
}

output "OUT_MAP_1" {
value = var.MAP_1["keyC"]
}

output "OUT_MAP_1_ALL_VALUES" {
value = var.MAP_1
}

# **** MAP, but now per Element in the Map
module "MAP_PER_ELEMENT" {
source = "./module_map_element"
for_each = var.MAP_1
mapElementKey = each.key
mapElementValue = each.value
}

output "MODULE_MAP_ELEMENT_OUTPUT" {
value = module.MAP_PER_ELEMENT
}

resource "null_resource" "this" {
for_each = var.MAP_1
xx = each.key
}

# **** LIST
variable "LIST_1" {
type = list(string)
default = [
"ValueA",
"ValueB",
"ValueC"
]
}

output "OUT_LIST_1_INDEX_2" {
value = var.LIST_1[2]
}

output "OUT_LIST_1_ALL_VALUES_IN_A_FOR" {
value = [for i in var.LIST_1 : i]
}

output "OUT_LIST_1_ALL_VALUES" {
value = var.LIST_1
}

# **** LIST, but now per element in the list
module "MODULE_LIST" {
source = "./module_list_element"
count = length(var.LIST_1)
LIST_ELEMENT = var.LIST_1[count.index]
}

output "MODULE_LIST_ELEMENT_OUTPUT" {
value = module.MODULE_LIST
}
output "MODULE_LIST_ELEMENT_OUTPUT_SECOND_VALUE" {
value = module.MODULE_LIST[2].MODULE_LIST_ELEMENT_OUTPUT
}
output "MODULE_LIST_ELEMENT_OUTPUT_ALL_IN_LIST" { # Return all values in a list
value = module.MODULE_LIST[*].MODULE_LIST_ELEMENT_OUTPUT
}


# **** LIST of Maps
variable "LIST_OF_MAPS" {
type = list(map(string))
default = [
{
city = "Amsterdam"
country = "Netherlands"
},
{
city = "New York"
country = "United States of America"
},
{
city = "London"
country = "United Kingdom"
}
]
}

# ** Get 'country' of second element
output "LISTMAP_COUNTRY_OF_ELEMENT_2" {
value = var.LIST_OF_MAPS[1].country
}
output "LISTMAP_ALL_COUNTRIES_1" {
value = [for i in var.LIST_OF_MAPS: i.country]
}
output "LISTMAP_ALL_COUNTRIES_2" {
value = var.LIST_OF_MAPS.*.country
}

# ** LIST of MAPS in a Module
module "LIST_OF_MAPS" {
source = "./module_listofmap"
count = length(var.LIST_OF_MAPS)
LISTOFMAP_ELEMENT = var.LIST_OF_MAPS[count.index]
}

output "ALL_COUNTRIES" {
value = module.LIST_OF_MAPS[*].COUNTRY
}

module_list_element/main.tf

variable "LIST_ELEMENT" {
type = string
}

output "MODULE_LIST_ELEMENT_OUTPUT" {
value = var.LIST_ELEMENT
}

module_map_element/main.tf

variable "mapElementKey" {
type = string
}

variable "mapElementValue" {
type = string
}

output "MODULE_MAP_ELEMENT_OUTPUT" {
value = "${var.mapElementKey} = ${var.mapElementValue}"
}

module_listofmap/main.tf

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

output "COUNTRY" {
value = var.LISTOFMAP_ELEMENT.country
}

Deployment

  • Create new directory
$ mkdir terraformlistandmap
$ cd terraformlistandmap
  • Create file main.tf in that directory
  • create subdirectories for the modules
$ mkdir module_listofmap module_map_element module_list_element
  • Create all the main.tf files in those subdirectories
  • run terraform init
$ terraform init
  • apply the configuration and see the output
$ terraform apply --auto-aprove