繁体   English   中英

Terraform对S3存储桶使用现有策略

[英]Terraform use existing policy for s3 bucket

在我的terraform配置中,我有一个附加到某些rolespolicy
创建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创建中引用该策略?

您可以通过多种方式实现这一目标。 您可以拥有策略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"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM