如何在 terraform 中遍历本地的多个值?

how to interate through multiple values in locals in terraform?

我想在 terraform 中创建一个 iam 策略,这样我就可以编写策略语句,如下所示,但我想创建一个变量(例如数组)并遍历它以通过 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
}

您可以使用 for_each,这将为您的每个 local.resources:

创建 allowpolicy
resource "aws_iam_policy" "allowpolicy" {
   name= "resourcesaccessallowed"
   for_each = local.resources   
}