Terraform 101: for_each usage

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 t…


This content originally appeared on Level Up Coding - Medium and was authored by Jiadong Chen

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.


This content originally appeared on Level Up Coding - Medium and was authored by Jiadong Chen


Print Share Comment Cite Upload Translate Updates
APA

Jiadong Chen | Sciencx (2024-06-21T14:12:50+00:00) Terraform 101: for_each usage. Retrieved from https://www.scien.cx/2024/06/21/terraform-101-for_each-usage/

MLA
" » Terraform 101: for_each usage." Jiadong Chen | Sciencx - Friday June 21, 2024, https://www.scien.cx/2024/06/21/terraform-101-for_each-usage/
HARVARD
Jiadong Chen | Sciencx Friday June 21, 2024 » Terraform 101: for_each usage., viewed ,<https://www.scien.cx/2024/06/21/terraform-101-for_each-usage/>
VANCOUVER
Jiadong Chen | Sciencx - » Terraform 101: for_each usage. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/21/terraform-101-for_each-usage/
CHICAGO
" » Terraform 101: for_each usage." Jiadong Chen | Sciencx - Accessed . https://www.scien.cx/2024/06/21/terraform-101-for_each-usage/
IEEE
" » Terraform 101: for_each usage." Jiadong Chen | Sciencx [Online]. Available: https://www.scien.cx/2024/06/21/terraform-101-for_each-usage/. [Accessed: ]
rf:citation
» Terraform 101: for_each usage | Jiadong Chen | Sciencx | https://www.scien.cx/2024/06/21/terraform-101-for_each-usage/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.