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
:
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" {
}
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
}
}
bucket
(String) The name of the Amazon S3 bucket to which the policy applies.policy_document
(String) A policy document containing permissions to add to the specified bucket. In IAM, you must provide policy documents in JSON format. However, in CloudFormation you can provide the policy in JSON or YAML format because CloudFormation converts YAML to JSON before submitting it to IAM. For more information, see the AWS::IAM::Policy PolicyDocument resource description in this guide and Access Policy Language Overview in the Amazon S3 User Guide.id
(String) Uniquely identifies the resource.Import is supported using the following syntax:
$ terraform import awscc_s3_bucket_policy.example <resource ID>