【问题标题】:Terraform use existing policy for s3 bucketTerraform 使用 s3 存储桶的现有策略
【发布时间】:2019-05-28 16:15:19
【问题描述】:

在我的 terraform 配置中,我有一个 policy 附加到一些 roles
创建 s3 存储桶时如何重用此策略?


resource "aws_iam_policy" "s3-read-access" {
  name   = "my-warehouse-read-access"
  version = "2019-05-28"
  policy = "${data.aws_iam_policy_document.s3-read-access.json}"
}

resource "aws_s3_bucket" "my-warehouse" {
  bucket = "my-bucket"
  acl    = "private"
  policy = "${aws_iam_policy.s3-read-access.arn}"
}

很遗憾,我收到一个错误:Error putting S3 policy: MalformedPolicy: Policies must be valid JSON and the first byte must be '{'

似乎policy 需要heredoc-notation 中的 json 配置,但我必须重新使用现有策略。
如何在创建 s3-bucket 时引用该策略?强>

【问题讨论】:

    标签: amazon-web-services amazon-s3 terraform


    【解决方案1】:

    您有多种方法可以实现这一目标。您可以拥有一个策略 JSON 并在每个存储桶中引用它:

    resource "aws_s3_bucket" "b" {
      bucket = "s3-website-test.hashicorp.com"
      acl    = "public-read"
      policy = "${file("policy.json")}"
    }
    

    或者你可以创建一个数据块:

    data "aws_iam_policy_document" "your_super_amazing_policy" {
     count  = "${length(keys(var.statement))}"
    
      statement {
        sid       = "CloudfrontBucketActions"
        actions   = ["s3:GetObject"]
        resources = ["*"]
      }
    

    还有你在桶上:

    resource "aws_s3_bucket" "private_bucket" {
      bucket = "acme-private-bucket"
      acl = "private"
      policy = "${data.aws_iam_policy_document.your_super_amazing_policy.json}"
    
      tags {
        Name = "private-bucket"
        terraform = "true"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 2021-04-04
      • 2022-11-03
      • 2019-05-15
      • 2021-10-21
      • 2021-03-29
      • 2023-03-31
      • 2011-09-10
      相关资源
      最近更新 更多