Here is the pictorial representation of the above list variable -
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) -
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
}
3. for loop
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 - user1, user2, user3
provider"aws" {
region="us-east-1"
}
variable"user_names" {
description="IAM usernames"
type=list(string)
default=["user1", "user2", "user3"]
}
output"print_the_names" {
value=[forname in var.user_names : name]
}
How to iterate over MAP?
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=[forname, role in var.iam_users : "${name} is the ${role}"]
}
Here is the difference between list and map syntax
For list -
{for <ITEM> in <LIST> : <OUTPUT_KEY> => <OUTPUT_VALUE>}
BASH
For Map -
{for <KEY>, <VALUE> in <MAP> : <OUTPUT_KEY> => <OUTPUT_VALUE>}
Azure Repository is a set of version control tools that we can use to manage our code. In case we are entirely new to version control, the...
Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker file with buildkit,1,Docker file with buildx,1,Docker Image Scan,1,Docker Quiz,1,Docker Quizzes,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,headless service
DNS service record,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant
kubernetes,1,Windows,1,YAML Basics,1,
Loaded All PostsNot found any postsVIEW ALLReadmoreReplyCancel replyDeleteByHomePAGESPOSTSView AllRECOMMENDED FOR YOULABELARCHIVESEARCHALL POSTSNot found any post match with your requestBack HomeSundayMondayTuesdayWednesdayThursdayFridaySaturdaySunMonTueWedThuFriSatJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecemberJanFebMarAprMayJunJulAugSepOctNovDecjust now1 minute ago$$1$$ minutes ago1 hour ago$$1$$ hours agoYesterday$$1$$ days ago$$1$$ weeks agomore than 5 weeks agoFollowersFollowTHIS PREMIUM CONTENT IS LOCKEDSTEP 1: Share to a social networkSTEP 2: Click the link on your social networkCopy All CodeSelect All CodeAll codes were copied to your clipboardCan not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copyTable of Content
COMMENTS