AWS Amazon S3 Bucket Policy
This page shows how to write Terraform and CloudFormation for Amazon S3 Bucket Policy and write them securely.
aws_s3_bucket_policy (Terraform)
The Bucket Policy in Amazon S3 can be configured in Terraform with the resource name aws_s3_bucket_policy
. The following sections describe 3 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_s3_bucket_policy" "allowActionsFromAllPrincipals" {
bucket = "test"
policy = <<POLICY
{
"Version": "2012-10-17",
resource "aws_s3_bucket_policy" "policy_statement_allow_action_without_wildcard" {
bucket = aws_s3_bucket.test_bucket.id
policy = <<EOF
{
"Version": "2012-10-17",
resource "aws_s3_bucket_policy" "policy_statement_allow_principal_without_wildcard" {
bucket = aws_s3_bucket.test_bucket.id
policy = <<EOF
{
"Version": "2012-10-17",
Parameters
Explanation in Terraform Registry
Attaches a policy to an S3 bucket resource.
Tips: Best Practices for The Other AWS Amazon S3 Resources
In addition to the aws_s3_bucket_public_access_block, AWS Amazon S3 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_s3_bucket_public_access_block
Ensure S3 bucket-level Public Access Block restricts public bucket policies
It is better to enable S3 bucket-level Public Access Block if you don't need public buckets.
aws_s3_bucket
Ensure S3 bucket access policy is well configured
It is better to configure the S3 bucket access policy properly to limit it unless explicitly required.
AWS::S3::BucketPolicy (CloudFormation)
The BucketPolicy in S3 can be configured in CloudFormation with the resource name AWS::S3::BucketPolicy
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref PackageBucket
PolicyDocument:
Version: '2012-10-17'
Statement:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref BucketForGeneralOperations
PolicyDocument:
Statement:
# allow the dev account to read from this bucket
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref BucketForGeneralOperations
PolicyDocument:
Statement:
- Action:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref Bucket
PolicyDocument:
Version: '2012-10-17'
Statement:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref Bucket
PolicyDocument:
Version: '2012-10-17'
Statement:
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "S3Bucket"
},
"PolicyDocument": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "S3Bucket"
},
"PolicyDocument": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "S3Bucket"
},
"PolicyDocument": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "S3Bucket"
},
"PolicyDocument": {
"Type" : "AWS::S3::BucketPolicy",
"Condition": "ConfigureAcceptanceTestLogsBucket",
"Properties" : {
"PolicyDocument" : {
"Statement" : [
{
Parameters
-
Bucket
required - String -
PolicyDocument
required - Json
Explanation in CloudFormation Registry
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 a403 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 a405 Method Not Allowed
error.Important 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
:+ CreateBucket + DeleteBucket
Frequently asked questions
What is AWS Amazon S3 Bucket Policy?
AWS Amazon S3 Bucket Policy is a resource for Amazon S3 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon S3 Bucket Policy?
For Terraform, the kanchwala-yusuf/aws-terraform, stelligent/config-lint and stelligent/config-lint source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the arielschvartz/serverless-cloudformation-cicd-template, vkushnirenko-luxoft/awscli and Chiru-Darshan/AWS-Development source code examples are useful. See the CloudFormation Example section for further details.