How to Make a Loop in Terraform

Terraform offers a declarative language model. Although this makes it very easy and efficient to define resources, it can lack some fundamental features useful in daily operations.

For example, specific declarative languages do not support control flow, such as loops. Therefore, when you need to create a piece of code repetitively, you will have to default to copy and paste. This is not a very efficient method for any developer.

Terraform understands this and provides a few features that help with features, such as conditionals and loops. In this article, you will learn how to use the count, for_each and for expressions in Terraform.

The Basics

Terraform provides three main structures when working with loops. These include:

  1. Count
  2. For_Each
  3. For

Let us understand how to use each of these loop constructs.

Count Parameter

The resource block in Terraform mainly defines and manages a single object in the cloud. However, you may need to create or manage multiple objects without creating each block for every object. Doing this removes repetitive code, thus making it easier to read and operate.

One way to accomplish this is the use of the count meta-argument. If you define a resource block that includes the count parameter with the value as a whole number, Terraform will perform the actions on the specified number of resources.

Basic Usage

The count parameter, also known as meta-argument, can be used in the resource or module blocks.

It accepts an integer or whole number and will perform the specified actions on the many selected instances. For example, if you set the count to 5 on a resource block that creates S3 buckets, Terraform will create five S3 bucket instances.

However, Terraform will create a unique infrastructure object for each item. Hence, each item receives CRUD operations separately.

The following example shows how to use the count parameter in Terraform:

terraform {
required_providers {
aws = {
            source = "hashicorp/aws"
            version = "-> 3.27"
required_version = ">= 0.12"
provider "aws" {
 profile ="default"
    region = "us-east-1"
resource "aws_instance""web_server" {
amo = "ami-234c45e2"
instance_type = "t1.micro"
    count = 3
    tags = {
        Name = "Webserver version: ${count.index}"

The example code above creates three AWS instances of type t1.micro. We also use the count method to create tags with the number of instances.

The index attribute allows you to modify the configuration for each item. The count index is a distinct value that describes each instance starting at 0.

Unlike most Terraform meta-arguments, the value of the count parameter must be known beforehand.

For_Each Parameter

The for_each parameter is closely similar to the count argument. You can use it with a resource and module block.

Basic Usage

The for_each parameter accepts a set of strings or a map. Then, it creates a specific instance for each item in the collection. Similar to count, each item has a unique object infrastructure id allowing Terraform to perform CRUD operations individually on a specific object.

The following snippet shows how to use the for_each item to create multiple users:

resource"aws_iam_user""web-managers" {
for_each = toset(["John", "Lucy", "Harley", "David"])
    name = each.key

The previous example shows how to use the for_each with a set of strings. To use a map, consider the code shown below:

for_each = {
 group = "web"
group_b = "db"
    name = each.key
    location = each.value

For Parameter

The for loop parameter is not very different from the for operation in most programming languages. It allows you to loop over a list of items.

For example, suppose you have a list of users, and you want to convert the usernames to uppercase. In such as case, you can use a for loop:

usernames = tolist(["username1", "username2", username3])
[foriinusernames.list : upper(s)]

The previous code expands the items in the list and converts each to uppercase. Learn more about Terraform for expressions in the documentation.


This guide gives you the basics of performing loop operations in Terraform using count, for_each, and for expressions. We hope you found the examples using the three parameters helpful. Check out Linux Hint for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list