Home > Mobile >  how to interate through multiple values in locals in terraform?
how to interate through multiple values in locals in terraform?

Time:03-19

I want to create an iam policy in terraform , such that I can write policy statement , such as below, but I would like to create a variable such as array and iterate through that to create policy for each resource via terraform.

   "Effect": "Allow", 
    "Action": "*"
    "Resources" : 
locals{
  resources = ["lambda", "s3" , "ec2"]
}
resource "aws_iam_policy" "allowpolicy" {
   name= "resourcesaccessallowed"
   
   #iterate through the resources list here in locals
}

CodePudding user response:

You can use for_each which will create allowpolicy for each of your local.resources:

resource "aws_iam_policy" "allowpolicy" {
   name= "resourcesaccessallowed"
   for_each = local.resources   
}
  • Related