Terraform Loops (for, for each)

 


As the name suggests we need to use count but to use the count first we need to declare collections inside our terraform file.

Let's create a collection variable of type list(string) -

variable "user_names" {
  description = "IAM usernames"
  type        = list(string)
  default     = ["user1", "user2", "user3"]
}
BASH

terraform loop and for_each loop

In the above collection, we have created a list of type string which contains usernames and these usernames we are going to use for creating aws_iam_user.

The code snippet shows how we are going to iterate over the list(string) -

resource "aws_iam_user" "example" {
  count = length(var.user_names)
  name  = var.user_names[count.index]
}
BASH

terraform loop and for_each loop

Here is the complete terraform file -

variable.tf
variable "instance_type" {
  type = string
  default = "t2.micro"
}
variable "ami" {
  type = string
  default ="ami-05fa00d4c63e32376"
}
variable "key_name" {
  type = string
  default = "newkey"
}
variable "region" {
  type = string
  default = "us-east-1"
}
variable "instances" {
  type = list(string)
  default = [ "WebServer" , "DbServer" ]
}

main.tf
BASH
provider "aws" {
  region ="us-east-1"
}

resource "aws_instance" "ins1" {
  count= length(var.instances)
  ami = var.ami
  instance_type = var.instance_type
  tags = {
    "Name" = var.instances[count.index]
  }
}


The for_each is a little special in terraforming and you can not use it on any collection variable.

Note : - It can only be used on set(string) or map(string).

The reason why for_each does not work on list(string) is because a list can contain duplicate values but if you are using set(string) or map(string) then it does not support duplicate values.

Let's first create a set(string) variable -

provider "aws" {
  region ="us-east-1"
}

variable "user_names" {
  description = "IAM usernames"
  type        = set(string)
  default     = ["user1", "user2", "user3"]
}
resource "aws_iam_user" "name" {
   for_each = var.user_names
   name = each.value
}

The for loop is pretty simple and if you have used any programming language before then I guess you will be pretty much familiar with the for loop.

Only the difference you will notice over here is the syntax in Terraform.

I am going to take the same example by declaring a list(string) and adding three users to it - user1user2user3

provider "aws" {
  region ="us-east-1"
}

variable "user_names" {
  description = "IAM usernames"
  type        = list(string)
  default     = ["user1", "user2", "user3"]
}
output "print_the_names" {
  value = [for name in var.user_names : name]
}

We can use a similar approach to iterate over the map also. But always keep in mind you need to specify the type of the map like string or number.

Here is the same example which I have taken but modified a bit for map -

provider "aws" {
  region ="us-east-1"
}

variable "iam_users" {
  description = "map"
  type        = map(string)
  default     = {
    user1      = "normal user"
    user2  = "admin user"
    user3 = "root user"
  }
}
output "user_with_roles" {
  value = [for name, role in var.iam_users : "${name} is the ${role}"]
}

For list -

{for <ITEM> in <LIST> : <OUTPUT_KEY> => <OUTPUT_VALUE>}
BASH

For Map -

{for <KEY>, <VALUE> in <MAP> : <OUTPUT_KEY> => <OUTPUT_VALUE>} 

Comments