【问题标题】:Build a S3 bucket policy with terraform dynamic blocks使用 terraform 动态块构建 S3 存储桶策略
【发布时间】:2021-10-21 17:03:06
【问题描述】:

我正在尝试创建一个 S3 存储桶策略以提供对许多其他帐户的访问权限。我不知道如何使用 Terraform 使用 for 循环或动态块。

locals {
  account_ids = [
    987654321098,
    765432109876,
    432109876543
  ]
}

resource "aws_s3_bucket_policy" "bucket" {
  bucket = aws_s3_bucket.bucket.id

  policy = jsonencode({
    Statement = [
      for account in local.account_ids : {
          Effect    = "Allow"
          Action    = [ ... ]
          Principal = { AWS = [ "arn:aws:iam::${account}:root" ] }
          Resource  = "${aws_s3_bucket.bucket.arn}/states/${account}/*"
        }
      ]
    }
  })
}

这失败了:错误:缺少参数分隔符/需要逗号来分隔每个函数参数。

如果我尝试使用动态块,则会出现类似问题。

最终我希望Statement 块包含 3 个块的列表,每个帐户一个。

有什么想法吗?

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws


    【解决方案1】:

    你的右括号太多了。应该是:

    resource "aws_s3_bucket_policy" "bucket" {
      bucket = aws_s3_bucket.bucket.id
    
      policy = jsonencode({
        Statement = [
          for account in local.account_ids : {
              Effect    = "Allow"
              Action    = [ ... ]
              Principal = { AWS = [ "arn:aws:iam::${account}:root" ] }
              Resource  = "${aws_s3_bucket.bucket.arn}/states/${account}/*"
            }
          ]
        })
    }
    

    【讨论】:

    • 哎哟!很好的定位。我花了很多时间试图为动态块制定正确的语法,这是一个额外的括号:-/谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 2021-08-14
    • 1970-01-01
    • 2019-05-15
    • 2021-04-04
    • 2020-03-29
    • 2023-03-31
    • 2011-09-10
    相关资源
    最近更新 更多