Reputation: 55
So I know I can do this by just doing the resource block over and over again until I write them all out, but it seems kind of redundant. Now I'm just trying to get the roles created before attaching the policies to the roles. I looked on here and throughout the web and I couldn't find anything that seemed to do it. I looked at the aws_iam_role module source code but I didn't see how they did it for multiple roles. Any assistance would be appreciated.
example code of what I do now.
resource "aws_iam_role" "test1" {
name = "role1"
assume_role_policy = file("${path.module}/json_files/assume_role.json")
}
resource "aws_iam_role" "test2" {
name = "role2"
assume_role_policy = file("${path.module}/json_files/assume_role.json")
}
resource "aws_iam_role" "test3" {
name = "role3"
assume_role_policy = file("${path.module}/json_files/assume_role.json")
}
Upvotes: 1
Views: 1160
Reputation: 238199
You either use count or for_each:
with count:
resource "aws_iam_role" "test" {
count = 3
name = "role${each.index}"
assume_role_policy = file("${path.module}/json_files/assume_role.json")
}
with for_each:
resource "aws_iam_role" "test" {
for_each = toset(["1", "2", "3"])
name = "role${each.key}"
assume_role_policy = file("${path.module}/json_files/assume_role.json")
}
Upvotes: 3