The for_each argument in Terraform allows you to create multiple instances of a resource based on a map or a set of strings. It creates a direct association between a resource instance and an element in your set or map.

Here’s how you can use for_each to create multiple instances of a resource in different Azure regions:

variable “regions” {
description = “A set of regions”
type = set(string)
default = [“australiaeast”, “australiasoutheast”, “australiacentral”]}locals {
naming = {
australiaeast = “aue”
australiasoutheast = “aus”
australiacentral = “auc”
}
}

resource “azurerm_resource_group” “this” {
for_each = var.regions
name = “rg-${local.naming[each.key]}-example-001”
location = each.key
}

resource “azurerm_storage_account” “this” {
for_each = azurerm_resource_group.this
name = “sta${local.naming[each.value.location]}example001”
resource_group_name = each.value.name
location = each.value.location
account_tier = “Standard”
account_replication_type = “LRS”
}To explore the complete code for this case, visit the repository here.

In the azurerm_resource_group block:

resource “azurerm_resource_group” “this” {
for_each = var.regions
name = “rg-${local.naming[each.key]}-example-001”
location = each.key
}

for_each is set to var.regions, which should be a map or set of regions. Terraform will create an instance of azurerm_resource_group for each element in var.regions. The each.key and each.value are used to access the key and value of each element in the map or set.

In the next block:

resource “azurerm_storage_account” “this” {
for_each = azurerm_resource_group.this
name = “sta${local.naming[each.value.location]}example001”
resource_group_name = each.value.name
location = each.value.location
account_tier = “Standard”
account_replication_type = “LRS”
}

for_each is set to azurerm_resource_group.this, which is a map of the resource groups created in the previous step. Terraform will create an instance of azurerm_storage_account for each resource group. The each.value object contains the attributes of each resource group, which are used to set the resource_group_name and location arguments for each storage account.

Run terraform plan followed by terraform apply to deploy the resources to Azure. The following screenshot demonstrates the successful creation of three storage account instances, each located in a different region.

Thank you for reading! To explore the complete code for this case, visit the repository here. I hope you find it useful!

Terraform 101: for_each usage was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

​ Level Up Coding – Medium

about Infinite Loop Digital

We support businesses by identifying requirements and helping clients integrate AI seamlessly into their operations.

Gartner
Gartner Digital Workplace Summit Generative Al

GenAI sessions:

  • 4 Use Cases for Generative AI and ChatGPT in the Digital Workplace
  • How the Power of Generative AI Will Transform Knowledge Management
  • The Perils and Promises of Microsoft 365 Copilot
  • How to Be the Generative AI Champion Your CIO and Organization Need
  • How to Shift Organizational Culture Today to Embrace Generative AI Tomorrow
  • Mitigate the Risks of Generative AI by Enhancing Your Information Governance
  • Cultivate Essential Skills for Collaborating With Artificial Intelligence
  • Ask the Expert: Microsoft 365 Copilot
  • Generative AI Across Digital Workplace Markets
10 – 11 June 2024

London, U.K.