OVERVIEW OF TERRAFORM FOR EACH LOOPS:

Terraform’s for_each loop feature allows you to dynamically create multiple instances of resources or sub-blocks within a configuration. This feature provides flexibility and simplifies the management of repetitive infrastructure components. By using for_each loops within sub-blocks, you can iterate over a collection of values and generate configurations for each item in the collection.

BENEFITS OF TERRAFORM FOR EACH LOOPS:

  • Code Simplification: for_each loops eliminate the need for repetitive resource or sub-block definitions. Instead, you define a single resource or sub-block block and use the loop to generate multiple instances based on the provided collection.
  • Dynamic Configuration: With for_each, you can easily scale your infrastructure by adding or removing items in the collection. This enables dynamic configuration and simplifies management as the number of resources or sub-blocks changes.
  • Targeted Changes: When making updates or modifications, for_each allows you to apply changes to specific instances of resources or sub-blocks. This fine-grained control reduces the risk of unintended changes and provides more flexibility in managing infrastructure updates.

EXAMPLE USAGE:

Let’s consider an example where you need to create multiple Amazon EC2 instances using for_each loops within a sub-block. Here’s how you can achieve this:

1. Defining a Collection:

Create a variable that holds a map or set of values representing the instances you want to create. For example:

variable “ec2_instances” {
    type = map(object({
    instance_type = string
    ami           = string
    }))
    default = {
        instance1 = {
            instance_type = t2.micro
            ami           = ami-0c94855ba95c71c99
        }
        instance2 = {
            instance_type = t2.nano
            ami           = ami-0123456789abcdef0
        }
    }
}
 

This variable defines a map with two instances, each containing an instance type and an AMI ID.

2. Using for_each loop:

Within your resource or sub-block definition, utilize the for_each loop to iterate over the instances defined in the variable. For example:

resource “aws_instance” “ec2_instance” {
    for_each = var.ec2_instances
    instance_type = each.value.instance_type
    ami           = each.value.ami
    # other EC2 instance configuration…
}

This configuration uses the for_each loop to create multiple EC2 instances based on the items in the ec2_instances variable. The loop iterates over each item, assigning the instance type and AMI ID to the corresponding attributes.

3. Applying Changes:

To apply the changes and create the EC2 instances defined in the loop, run the terraform apply command.

$ terraform apply

This command applies the changes defined in the main configuration, creating multiple EC2 instances based on the values provided in the ec2_instances variable.

4. Modifying Instances:

To make changes to a specific instance, refer to it using the resource name and the instance key from the for_each loop. For example, to modify the “instance1” instance, you can update its attributes in the configuration file:

resource “aws_instance” “ec2_instance” {
    for_each = var.ec2_instances
    instance_type = each.value.instance_type
    ami           = each.value.ami
    # other EC2 instance configuration…
}

 

FINAL THOUGHTS:

By utilizing for_each loops, it allows your team to simplify repeated infrastructure components and organize your infrastructure as code in a more pragmatic fashion. Instead of repeatedly defining a set of Ec2 instances, for example, you can simply dynamically define the variables required to spin up each of those instances in your code.  If you’re just getting started on your journey into infrastructure as code, start the right way, follow this link to get in contact with one of our experts.

We’ve helped a variety of organizations get started the right way on their journey into infrastructure as code.

For more technical how-to’s click here.