awscc_s3_bucket_policy (Resource)

Applies an Amazon S3 bucket policy to an Amazon S3 bucket. If you are using an identity other than the root user of the AWS-account that owns the bucket, the calling identity must have the PutBucketPolicy permissions on the specified bucket and belong to the bucket owner's account in order to use this operation. If you don't have PutBucketPolicy permissions, Amazon S3 returns a 403 Access Denied error. If you have the correct permissions, but you're not using an identity that belongs to the bucket owner's account, Amazon S3 returns a 405 Method Not Allowed error. As a security precaution, the root user of the AWS-account that owns a bucket can always use this operation, even if the policy explicitly denies the root user the ability to perform this action. For more information, see Bucket policy examples. The following operations are related to PutBucketPolicy:

Example Usage

Deny public read

Deny read object from any principles

resource "awscc_s3_bucket_policy" "example" {
  bucket = awscc_s3_bucket.example.id
  policy_document = jsonencode({
    "Version" : "2012-10-17",
    "Statement" : [
      {
        "Sid" : "DenyPublicRead",
        "Effect" : "Deny",
        "Principal" : "*",
        "Action" : "s3:GetObject",
        "Resource" : "${awscc_s3_bucket.example.arn}/*"
      }
    ]
  })
}

resource "awscc_s3_bucket" "example" {
}

GET requests from specific referers

The following sample is a bucket policy that is attached to the DOC-EXAMPLE-BUCKET bucket and allows GET requests that originate from www.example.com and example.net

data "aws_caller_identity" "current" {}

resource "awscc_s3_bucket_policy" "example" {
  bucket = awscc_s3_bucket.example.id
  policy_document = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Action    = ["s3:GetObject"]
      Effect    = "Allow"
      Resource  = "${awscc_s3_bucket.example.arn}/DOC-EXAMPLE-BUCKET/*"
      Principal = "*"
      Condition = {
        StringLike = {
          "aws:Referer" = ["http://www.example.com/*", "http://example.net/*"]
        }
      }
    }]
  })
}

resource "awscc_s3_bucket" "example" {
  public_access_block_configuration = {
    block_public_acls       = true
    block_public_policy     = false
    ignore_public_acls      = true
    restrict_public_buckets = false
  }
}

Schema

Required

Read-Only

Import

Import is supported using the following syntax:

$ terraform import awscc_s3_bucket_policy.example <resource ID>