Home > front end >  Loop iam resources terraform
Loop iam resources terraform

Time:10-22

I'm trying to create a loop inside the resources bloc on a aws_iam_policy_document statement.

I do have a local variable accounts_to_protect which is a list of AWS's account's ID

locals {
  accounts_to_protect = tolist(setsubtract(var.all_accounts, var.blocked_accounts))
}

Currently, I just use the first index of my list

    resources = [
"arn:aws:ec2::${local.accounts_to_protect.0}:*"
]

I don't know how I can iterate it inside the resources block. I tried to add a for but it seems to not work. I would like to have a resource arn per account id.

CodePudding user response:

One possible solution is already in comments. The second one, would be:

    resources = [for account in local.accounts_to_protect: "arn:aws:ec2::${account}:*"]
  • Related