Managing Repetitive Infrastructure with for_each and count in Terraform

Managing Repetitive Infrastructure with for_each and count in Terraform

As the infrastructure grows in complexity, the need to deploy multiple instances of the same resource becomes imminent. Terraform offers two looping techniques, for_each and count, to tackle this. In this blog post, we explore how these techniques can be used to manage repetitive infrastructure with ease.

1. count:

count is a simple yet powerful inbuilt feature in Terraform that allows you to duplicate resources based on a numeric value. It is ideal for scenarios where you know the exact number of resource instances you need to create. For instance, deploying multiple identical EC2 instances for load balancing or autoscaling purposes.

resource "aws_instance" "example" {
  count         = 3
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

In this example, Terraform will create three EC2 instances with the specified AMI and instance type. We can update the count value to any other number to change the number of instances accordingly.

2. for_each:

While count is great for a fixed number of resources, but it falls short when dealing with dynamic or variable-driven infrastructure. Here is when for_each comes into the picture, as it is more flexible and suited for use cases where you need to create resources based on data structures such as maps or sets.

variable "ec2_instances" {
  type = map(object({
    ami           = string
    instance_type = string
  }))
  default = {
    instance1 = {
      ami           = "ami-0c55b159cbfafe1f0"
      instance_type = "t2.micro"
    }
    instance2 = {
      ami           = "ami-0c55b159cbfafe1f0"
      instance_type = "t2.small"
    }
  }
}

resource "aws_instance" "example" {
  for_each = var.ec2_instances

  ami           = each.value.ami
  instance_type = each.value.instance_type
}

In this example, we define a variable ec2_instances as a map of objects containing AMI and instance type details. By using for_each, Terraform creates two EC2 instances based on the data provided in the variable. This way, we can easily add or remove instances by updating the map.

3. When to use what - count or for_each ?

The decision to use count or for_each depends on your infrastructure requirements. If you know the exact number of resources you need, count is a straightforward choice. However, if you want greater flexibility and intend to create resources based on dynamic input, for_each is the way to go.

4. A few points to note:

Keep in mind that while count and for_each are powerful tools, they can have implications on your Terraform state and resource management. Changing the value of count or the keys within a for_each map can lead to resource destruction or recreation. Therefore, it's important to be cautious when modifying these configurations in production environments.

In conclusion, Terraform's for_each and count are essential features for managing repetitive infrastructure effectively. With count, you can easily duplicate resources a fixed number of times, while for_each provides the flexibility to create resources based on dynamic input. With these looping techniques, infrastructure automation becomes a cakewalk, allowing you to focus on building complex but robust infrastructure.


If you liked what you read, do consider sharing the article with a friend and connect with me on Twitter - @afraz_momin. Also, subscribe to my newsletter and stay up-to-date with my latest blog posts.