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

aws_s3_bucket_policy.tf#L5
resource "aws_s3_bucket_policy" "allowActionsFromAllPrincipals" {
  bucket = "test"

  policy = <<POLICY
{
  "Version": "2012-10-17",
policy_statement_action_wildcard.tf#L6
resource "aws_s3_bucket_policy" "policy_statement_allow_action_without_wildcard" {
  bucket = aws_s3_bucket.test_bucket.id

  policy = <<EOF
{
  "Version": "2012-10-17",
policy_statement_principal_wildcard.tf#L6
resource "aws_s3_bucket_policy" "policy_statement_allow_principal_without_wildcard" {
  bucket = aws_s3_bucket.test_bucket.id

  policy = <<EOF
{
  "Version": "2012-10-17",

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

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.

risk-label

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.

risk-label

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.

Review your AWS Amazon S3 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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

s3.yml#L15
    Type: "AWS::S3::BucketPolicy"
    Properties:
      Bucket: !Ref BucketForGeneralOperations
      PolicyDocument:
        Statement:
            # allow the dev account to read from this bucket
Buckets.yml#L15
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref PackageBucket
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
s3-bucket.yml#L12
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref BucketForGeneralOperations
      PolicyDocument:
        Statement:
          - Action:
s3-sample.yml#L115
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket: !Ref Bucket
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
s3.yml#L115
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket: !Ref Bucket
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
s3_bucket_with_wildcards.json#L61
      "Type": "AWS::S3::BucketPolicy",
      "Properties": {
        "Bucket": {
          "Ref": "S3Bucket"
        },
        "PolicyDocument": {
s3_read_plus_list.json#L25
      "Type": "AWS::S3::BucketPolicy",
      "Properties": {
        "Bucket": {
          "Ref": "S3Bucket"
        },
        "PolicyDocument": {
bad_template.json#L22
      "Type": "AWS::S3::BucketPolicy",
      "Properties": {
        "Bucket": {
          "Ref": "S3Bucket"
        },
        "PolicyDocument": {
s3_bucket_with_wildcards.json#L22
      "Type": "AWS::S3::BucketPolicy",
      "Properties": {
        "Bucket": {
          "Ref": "S3Bucket"
        },
        "PolicyDocument": {
s3-for-cloudfoundry.json#L123
      "Type" : "AWS::S3::BucketPolicy",
      "Condition": "ConfigureAcceptanceTestLogsBucket",
      "Properties" : {
        "PolicyDocument" : {
          "Statement" : [
            {

Parameters

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 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.

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 vkushnirenko-luxoft/awscli, arielschvartz/serverless-cloudformation-cicd-template and Chiru-Darshan/AWS-Development source code examples are useful. See the CloudFormation Example section for further details.