Terraform Security:

Last Updated on August 7, 2025 by Arnav Sharma

Using for_each in Terraform is a powerful way to deploy multiple resources based on a set of inputs. This method is particularly useful when you want to create multiple instances of a resource with only slight variations between them. Here’s how you can use for_each to deploy multiple resources:

Step 1: Define Your Input Set

First, you need to define a set of inputs that for_each will iterate over. This can be a list, set, or a map. Maps are often used because they allow you to easily associate each resource with a unique key.

For example, suppose you want to create multiple storage accounts in Azure. You might start with a map defining each account:

variable "storage_accounts" {
  description = "A map of storage accounts to create"
  type        = map(object({
    location = string
    account_tier = string
    account_replication_type = string
  }))

  default = {
    "account1" = {
      location = "eastus"
      account_tier = "Standard"
      account_replication_type = "LRS"
    },
    "account2" = {
      location = "westus"
      account_tier = "Standard"
      account_replication_type = "GRS"
    }
    # Add more accounts as needed
  }
}

Step 2: Use for_each in Your Resource Configuration

Next, use for_each in your resource block to iterate over each element in your input set. For each element, Terraform will create a new instance of the resource.

resource "azurerm_storage_account" "example" {
  for_each = var.storage_accounts

  name                     = each.key
  location                 = each.value.location
  account_tier             = each.value.account_tier
  account_replication_type = each.value.account_replication_type

  # Other required fields...
}

In this example, each.key is the key from the storage_accounts map (e.g., “account1”, “account2”), and each.value is the corresponding value, which is an object with properties like location, account_tier, and account_replication_type.

Step 3: Initialize and Apply Your Terraform Configuration

  • Run terraform init to initialize the working directory.
  • Run terraform plan to see what resources Terraform will create or modify.
  • Run terraform apply to create the resources.

Additional Tips

  • Dynamic Blocks: For more complex scenarios, you might use dynamic blocks within resources to create nested structures based on a list or map.
  • Dependency Management: Be mindful of dependencies between resources. Terraform automatically handles dependencies, but in some cases, you may need to explicitly define them.
  • State Management: Each resource created with for_each will have a unique identifier in the Terraform state file based on its key in the map. This helps in managing each resource independently.
  • Modifying the Set: If you modify the input set (e.g., add or remove an item), Terraform will add, update, or destroy resources to match the new set.

Using for_each can significantly simplify your Terraform configurations, especially when dealing with multiple similar resources. It ensures that your configurations are more dynamic and easier to maintain.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.