How to Use Terraform For_each: A Comprehensive Guide in 2024 with Examples

terraform for_eachDo you also want to know about terraform for_each attribute. So let’s start by understanding about terraform first. Terraform is an open source tool. It is also under constant maintenance. Terraform is dynamic and always enables new features, as it is regularly updated by many programmers. You can also submit the features of your own to add something you think would be knowledgeable for the community.

Terraform is a powerful infrastructure-as-code tool that can utililze to automate the maintainance, creation, and destruction of cloud resources. It is likely to be  how programmers use repositories such as GitHub to store their code, now talking about terraform for_each attribute that offers many benefits of the version control system for an enterprises’ cloud infrastructure.

Terraform offers many features that encourage  best practices for programming , like code reuse and dynamic code blocks. In this article, you’ll learn about Terraform’s versatility as one of the infrastructure-as-code tools and also learn how to work with these useful feature of the terraform for_each attribute with examples.

Understanding the for_each Meta-ArgumentUnderstanding the for_each Meta-Argument

The terraform for_each meta-argument in Terraform is used for data structure, like for list or map, configuring resources and module blocks with each item. This dynamic block is useful when you have multiple similar resources, such as Kubernetes pods or virtual machines Kubernetes pods, that share the similar lifecycle but need other configurations. Terraform for_each is one of the looping constructs developed in version 0.13 known as count.

The for_each create multiple similar instances resource in a more flexible way. It works with a set of strings or a map or and creates an instance for each item in a map or set. Let’s see some of the examples of how for_each is used below.

Basic Usage of for_each

Simple List Example:

Here’s an example of using `for_each` in a Terraform configuration to create multiple AWS S3 buckets:

variable "buckets" {
type = map(object({
acl = string
force_destroy = bool

resource "aws_s3_bucket" "example" {
for_each = var.buckets

bucket = each.key
acl = each.value.acl

force_destroy = each.value.force_destroy

You can define your buckets in a `.tfvars` file like this:

buckets = {
bucket1 = {
acl = "private"
force_destroy = false
bucket2 = {
acl = "public-read"
force_destroy = true

How to use Terraformfor_eachattribute?

Generally a Terraform project’s code is paired up with the application along with the Terraform code that controls the infrastructure on which the application runs on.

To simplify the process of creating the same cloud resources many times, Terraform brings you the for_each argument. Passing the terraform for_each argument to the cloud resource tells it to alter a different resource for item listed in the map or list. Let’s know more about terraform for_each argument with some practical example:

Example: Using for_each to create a VPC resource

To illustrate, use a Terraform module to create a simple virtual private cloud (VPC) resource in AWS, as shown:

variable "vpcs" {
type = map(object({
cidr_block = string
instance_tenancy = string

resource "aws_vpc" "my_vpc" {
for_each = var.vpcs

cidr_block = each.value.cidr_block
instance_tenancy = each.value.instance_tenancy

tags = {
Name = each.key

2. Create the same VPC resource for multiple environments

To create the same VPC resource for multiple environments, you can utilize Terraform workspaces or manage separate state files for each environment. Here’s how you can do it using separate state files:

1. Create directories for each environment:

├── modules/
│ └── vpc/
│ ├──
│ ├──
│ └──
├── prod/
│ ├──
│ ├──
│ └── terraform.tfvars
└── dev/
└── terraform.tfvars


2. Define the VPC module in the module directory (`modules/vpc`), as described in the previous response.

3. Define the environment-specific configurations  in the `prod` and `dev` directories:


module "prod_vpc" {
source = "../modules/vpc"
cidr_block = var.prod_cidr_block
vpc_name = "prod-vpc"


variable "prod_cidr_block" {
description = "The CIDR block for the production VPC"


module "dev_vpc" {
source = "../modules/vpc"
cidr_block = var.dev_cidr_block
vpc_name = "dev-vpc"


variable "dev_cidr_block" {
description = "The CIDR block for the development VPC"

4. Define the environment-specific variable values in the respective `terraform.tfvars` files:


prod_cidr_block = ""


dev_cidr_block = ""

Benefits of Using Terraformfor_eachattributes 

1. Cut down unwanted duplication

Using the terraform for_each attribute can cut down on unwanted duplication in the Terraform configurations.

2. Less time taking and more efficient 

Adding new features or items to the environment list or altering existing items is very quick as all the main items for configurable are stored at one place.

benefits of terraform

3. Can develop different patterns with same setup

You can also use some different patterns to develop the same setup as in this example, lile using Terraform workspaces. However, for_each can be applied more widely to dynamically create resources depending on maps, lists, or other resources.

Limitations with Terraform for_each attributes 

for_each keys that is the keys of a map, or values that is the values in a set, used for iteration cater as identifiers for the multiple resources they develop . As such, they are visible in the terraform UI output that is terraform plan or terraform apply steps and also in the state file as well. Hence, sensitive values cannot be utilize as arguments in for_each implementations. The sensitive values that can’t be used are:

1. Sensitive input variables: These are variables set to true with the sensitive argument.

2. Sensitive outputs: These are outputs set to true with the sensitive argument.

3. Sensitive resource attributes: These are the attributes with sensitive data marked as sensitive using the built-in sensitive function.

You will get an error if you try using sensitive values as for_each arguments.

Also, the keys or values of a for_eachhave to be known before a terraform apply operation. These values or keys cannot also rely on the result of any unnecessary function such as  timestamp as they are evaluated later on during the main evaluation step. Also, use descriptive value or keys. Since terraform for_each values identify resources, uses meaningful keys or values that eventually helps to easily identify resources.

count vs. for_each

count vs for_eachWhile both count and for_each are used for looping in Terraform but offers different services. The count method is certainly used if you want to design a fixed number of identical resources. On the other hand, for_each is more convinient while dealing with resources that requires unique configurations.

It’s essential to note that count and terraform for_each are both exclusive, it means you cannot use them together for one single resource. However, there are workarounds to combine their functionalities.


Ỉn conclusions, you now might know about terraform and Terraform for_each and also seen several examples of how for_each can be utilized. Even though for_each gives you flexibility; you need to be mindful when using it, especially in large-scale deployments, to avoid performance degradation, as highlighted earlier.

The terraform for_each loop offers a powerful way to manage multiple resources effectively and effectively. By understanding its usage infrastructure engineers and developers can harness its full power to write Terraform code in more cleaner and maintainable way.

Frequently Asked Questions 

Q 1 : What is Terraform?

A: Terraform is one of the open-source infrastructure as code software tool that permits you to define and modify your cloud infrastructure resources by using a declarative configuration language. It is mainly developed by HashiCorp.

Q 2: How does Terraform work?

A: Terraform works by utilizing a declarative configuration language to define and describe the expected state of your infrastructure. It then develop an execution plan that is based on that description and applies the changes to your infrastructure resources to bring them to the desired result.

Q 3: What is the purpose of the meta-argument in Terraform?

A: A meta-argument in Terraform is a special type of argument used to modify the behavior of the module or resource. It provides additional functionality or customization options as well.

Q 4:  How can I deploy multiple AWS EC2 instances in Terraform?

A: To deploy multiple EC2 instances in Terraform, you can use the `count` or `for_each` meta-argument on your resource block. This permits you to define a set of strings or a map or a set of objects to create multiple similar resources.

Related Posts