Skip to content

Instantly share code, notes, and snippets.

@devops-school
Last active June 9, 2023 22:35
Show Gist options
  • Save devops-school/1f3efed15d390748b208a109f9765e0c to your computer and use it in GitHub Desktop.
Save devops-school/1f3efed15d390748b208a109f9765e0c to your computer and use it in GitHub Desktop.
Terraform variable Map Type Explained
Example 1
===============================
Maps are a way to create variables that are lookup tables. An example will show this best. Let's extract our AMIs into a map and add support for the us-west-2 region as well:
variable "amis" {
type = "map"
default = {
"us-east-1" = "ami-b374d5a5"
"us-west-2" = "ami-4b32be2b"
}
}
A variable can have a map type assigned explicitly, or it can be implicitly declared as a map by specifying a default value that is a map. The above demonstrates both.
Then, replace the aws_instance with the following:
resource "aws_instance" "example" {
ami = var.amis[var.region]
instance_type = "t2.micro"
}
Example 2
===============================
variable "plans" {
type = "map"
default = {
"5USD" = "1xCPU-1GB"
"10USD" = "1xCPU-2GB"
"20USD" = "2xCPU-4GB"
}
}
plan = "${var.plans["5USD"]}"
The values matching to their keys can also be used to look up information in other maps. For example, underneath is a short list of plans and their corresponding storage sizes.
variable "storage_sizes" {
type = "map"
default = {
"1xCPU-1GB" = "25"
"1xCPU-2GB" = "50"
"2xCPU-4GB" = "80"
}
}
These can then be used to find the right storage size based on the monthly price as defined in the previous example.
size = "${lookup(var.storage_sizes, var.plans["5USD"])}"
variable "set_password" {
default = false
}
Example 3
===============================
# Use Case of for_each
variable "account_name" {
type = "map"
default = {
"account1" = "devops1"
"account2" = "devops2"
"account3" = "devops3"
}
}
resource "aws_iam_user" "iamuser" {
for_each = var.account_name
name = "${each.value}-iam"
}
Example 4
=============================
variable "grps" {
type = map
default = {
one = "hello1"
two = "hello2"
}
}
resource "azurerm_resource_group" "mapdemo1" {
name = var.grps["one"]
location = "South India"
}
output "resource_group" {
value = azurerm_resource_group.mapdemo1.grps["one"]
}
Example 5
===============================
variable "grps" {
type = map
default = {
"one" = "hello1"
"two" = "hello2"
}
}
resource "azurerm_resource_group" "mapdemo1" {
name = var.grps["one"]
location = "South India"
}
output "resource_group" {
value = azurerm_resource_group.mapdemo1.grps["one"]
}
@minnieshi
Copy link

can you please give an example for variables which did not set up the default values, and type is 'map', how can it be declared?
and how it can be set from the caller for the values?

@lorenzoBrugnera
Copy link

Is it possible to reference environment variables inside a map type variable?

@ashishjullia
Copy link

FYI @devops-school

Examples 4 and 5 are identical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment