【问题标题】:How to loop through a list of s3 buckets and create and attach a number of policies for each bucket?如何遍历 s3 存储桶列表并为每个存储桶创建和附加许多策略?
【发布时间】:2021-03-16 22:27:51
【问题描述】:

我正在学习 terraform 模块,我的目标是构建接收 s3 Buckets 集合的模块,然后创建并应用一些 iam 策略。

到目前为止,我尝试过的是创建某种 for 循环,在其中我生成策略并将它们附加到存储桶。作为参考,我的代码如下所示:

data "aws_iam_policy_document" "foo_iam_policy" {
  statement {
    sid       = ""
    effect    = "Allow"
    resources = [
    for arn in var.s3_buckets_arn :
    "${arn}/*"
    ]

    actions = [
      "s3:GetObject",
      "s3:GetObjectVersion",
    ]
  }

  statement {
    sid       = ""
    effect    = "Allow"
    resources = var.s3_buckets_arn
    actions = ["s3:*"]
  }
}


resource "aws_iam_policy" "foo_iam_policy" {
  name        = "foo-iam-policy"
  path        = "/"
  description = "IAM policy for foo to access S3"
  policy      = data.aws_iam_policy_document.foo_iam_policy.json
}

data "aws_iam_policy_document" "foo_assume_rule_policy" {
  statement {
    effect  = "Allow"
    actions = [
      "sts:AssumeRole"]

    principals {
      type        = "AWS"
      identifiers = [
        var.foo_iam_user_arn]
    }
    condition {
      test     = "StringEquals"
      values   = var.foo_external_ids
      variable = "sts:ExternalId"
    }
  }
}

resource "aws_iam_role" "foo_role" {
  name               = "foo-role"
  assume_role_policy = data.aws_iam_policy_document.foo_assume_rule_policy.json
}

resource "aws_iam_role_policy_attachment" "foo_attach_s3_policy" {
  role       = aws_iam_role.foo_role.name
  policy_arn = aws_iam_policy.foo_iam_policy.arn
}

data "aws_iam_policy_document" "foo_policy_source" {
  for_each = toset(var.s3_buckets_arn)
  //  arn = each.key
  statement {
    sid    = "VPCAllow"
    effect = "Allow"

    resources = [
      each.key,
      "${each.key}/*",
    ]

    actions = [
      "s3:*"]

    condition {
      test     = "StringEquals"
      variable = "aws:SourceVpc"
      values   = [
        "vpc-01010101"]
    }

    principals {
      type        = "*"
      identifiers = [
        "*"]
    }
  }
}

我不知道我的尝试是否有意义,或者是否有更好的方法来循环遍历存储桶并生成策略。我的问题是:对于想要提供存储桶列表并循环遍历它们以附加策略的这种情况,最佳做法是什么?

顺便说一句,我的方法遇到了一个错误:

“for_each”值取决于资源属性,不能 确定(地形)

【问题讨论】:

    标签: amazon-web-services terraform hcl


    【解决方案1】:

    要将存储桶策略附加到存储桶,您应该使用aws_s3_bucket_policy,而不是aws_iam_policy_document。此外,如果存储桶已经存在,可能最好先获取数据使用数据源aws_s3_bucket

    data "aws_s3_bucket" "selected" {
      # s3_buckets_names easier to use then s3_buckets_arns 
      for_each = toset(var.s3_buckets_names)
    
      bucket = each.value
    }
    

    然后,您可以遍历选定的存储桶并将您的策略​​添加到其中:

    resource "aws_s3_bucket_policy" "bucket_policie" {
    
      for_each = data.aws_s3_bucket.selected
    
      bucket = each.key
    
      policy = "your policy document"
    }
    

    【讨论】:

    • 你好马辛。看起来不错!如果我希望我的策略动态包含存储桶的 ARN,或者至少实现通过存储桶循环的功能,例如:for arn in var.s3_buckets_arn : "${arn}/*"
    • @alt-f4 Arn 应该在each.value.arn
    猜你喜欢
    • 2021-08-14
    • 2021-01-19
    • 2019-05-15
    • 2020-11-05
    • 2012-11-23
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多