🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform Tutorials: Variables – Data Type – Map

In this example, the instance_tags variable is defined with a type of map(string), and it is given a default value of { Name = "my-instance" }. The AWS provider is then configured to use the us-west-2 region.

The aws_instance resource is used to launch Amazon EC2 instances. The tags argument is used to assign tags to the instance, and it is set to the value of the instance_tags variable.

You can change the value of instance_tags by modifying the default value in the variable definition, or you can specify a different value when you run Terraform by using the -var flag. For example, if you want to launch an instance with tags { Name = "my-instance", Environment = "prod" }, you can run the following command:

variable "instance_tags" {
type = map(string)
default = {
Name = "my-instance"
}
}
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = var.instance_tags
}
view raw main.tf hosted with ❤ by GitHub

$ terraform apply -var ‘instance_tags={ Name = “my-instance”, Environment = “prod” }’


Maps are a collection of string keys and string values. These can be useful for selecting values based on predefined parameters such as the server configuration by the monthly price.

We’ve replaced our sensitive strings with variables, but we still are hard-coding AMIs. Unfortunately, AMIs are specific to the region that is in use. One option is to just ask the user to input the proper AMI for the region, but Terraform can do better than that with maps.

dynamic lookup – The square-bracket index notation used here is an example of how the map type expression is accessed as a variable, with [var.region] referencing the var.amis declaration for dynamic lookup.

static value lookup – the map type expression can also use a static value lookup directly with var.amis[“us-east-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:

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"]
}

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.