DevOps | Scripts | Automation

Terraform

Terraform Map

Terraform map – What is map variables in terraform?

Map variables in the terraform are the commonly used variables in complex situations. You can get the list of variables used in Terraform from the Terraform site.

This article will create a storage account with a map variable.


How to use map variables?

Terraform map variable is like hashtables. It is a key and value combination. For example, below is the declaration for the map variable.

variable "storageProperties" {
   type = map
}

Here is how you can provide value in the tfvars file.

storageProperties = {
    location = "eastus"
    account_tier = "Standard"
    storageName = "mapstorageaccount"
    replication_type = "GRS"
}

In the above tfvars files, the left side items are called keys (i.e. location,storageName, etc.), and the right side items are called values (i.e. eastus, standard, etc(. There are some methods available to read the map variables. Let’s bring them into this article.

Read the specific value

To read the specific value, use the key of the variable.

var.variableName[Key]

See the main.tf file

resource "azurerm_resource_group" "resourceGroup" {
    name = "mapResourceGroup"
    location = "eastus"
}


resource "azurerm_storage_account" "StorageAccount" {
   name = var.storageProperties["storageName"]
   resource_group_name = azurerm_resource_group.resourceGroup.name
   location  = var.storageProperties["location"]
   account_tier = var.storageProperties["account_tier"]
   account_replication_type  = var.storageProperties["replication_type"]
} 

As you see in the main.tf file we are passing Keys to refer to the values from the tfvars file.


Iterate through each variable.

In some cases, you may need to create multiple properties then you can use the map variable with for_each. For example, Let’s say you need to create another storage account with the same properties above then we can modify the files as below.

variables.tf

variable storageProperties {type = map}
variable storageAccountNames {type = map}

terraform.tfvars

storageProperties = {
    location = "eastus"
    account_tier = "Standard"
    storageName = "mapstorageaccount"
    replication_type = "GRS"
}

storageAccountNames = {
    "storageAccount1" = "mapstorageaccount1"
    "storageAccount2" = "mapstorageaccount2"
    "storageAccount3" = "mapstorageaccount3"
}

main.tf

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.71.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "resourceGroup" {
    name = "mapResourceGroup"
    location = "eastus"
}


resource "azurerm_storage_account" "StorageAccount" {
   name = var.storageProperties["storageName"]
   resource_group_name = azurerm_resource_group.resourceGroup.name
   location  = var.storageProperties["location"]
   account_tier = var.storageProperties["account_tier"]
   account_replication_type  = var.storageProperties["replication_type"]
} 

resource "azurerm_storage_account" "multipleStorageAccounts" {
   for_each = var.storageAccountNames
   name = "${each.value}"
   resource_group_name = azurerm_resource_group.resourceGroup.name
   location  = var.storageProperties["location"]
   account_tier = var.storageProperties["account_tier"]
   account_replication_type  = var.storageProperties["replication_type"]
}

You can see that the MultipleStorageAccounts block creates multiple storage accounts with map variables by looping the values.

 for_each = var.storageAccountNames
 name = "${each.value}"

Output

Now what about Output, for the single storage account let’s say you need to get the storage account ID for the created storage.

output "storageAccountId" {
    value = azurerm_storage_account.StorageAccount.id
} 

For multiple storage accounts?
For the second module, we mentioned. There are multiple methods to get the output but I prefer the below method.

output "multiStorage_replicationType" {
    value = {for k, v in azurerm_storage_account.multipleStorageAccounts : k => v.id}
}

For the Specific storage account

output "specific_storageAccount" {
    value = azurerm_storage_account.multipleStorageAccounts["storageAccount1"].id
}

This code is also available in the GitHub repository.

Loading

One thought on “Terraform Map

Comments are closed.