Followers

Terraform Loops (for, for each)

  1.  Loops with count As the name suggests we need to use  count  but to use the  count  first we need to declare collections inside our te...

 

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>} 
BASH

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Terraform Loops (for, for each)
Terraform Loops (for, for each)
https://jhooq.com/wp-content/uploads/terraform/for-loop/terraform-loop.webp
DevOpsWorld
https://www.devopsworld.co.in/2022/09/terraform-loops-for-for-each.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/09/terraform-loops-for-for-each.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content