[Recipes] S3 Bucket Policy Examples - Basic Policies and Use of Principal

Problem: 

Understand bucket policy by experimenting with it.

Solution Summary: 

Bucket policies determine whether a user, group, or role is authorized to do an operation on a S3 resource. We can specify JSON based bucket policies for your buckets under Permissions tab. To make it easier to create policies, AWS also provides a policy generator.

Prerequisites: 

You need an AWS account and basic S3 familiarity.

Solution Steps: 

  1. Create a bucket (e.g. buddybucketpolicy) leaving all default selections as is.

  2. Create a text file and upload it with default settings.

  3. Verify that you get access denied warning when you try to access the file publicly.

  4. Go to policy generator and generate policy as per the given policy description or copy paste the given policy json in bucket policy (editor) under Permissions tab.

 

Examples

Policy 1

{

  "Id": "Policy1500985827175",

  "Version": "2012-10-17",

  "Statement": [

    {

      "Sid": "Stmt1500985820888",

      "Action": [

        "s3:GetObject"

      ],

      "Effect": "Allow",

      "Resource": "arn:aws:s3:::buddybucketpolicy/policytestfile.txt",

      "Principal": "*"

    }

  ]

}

 

Description: Allow everyone to access the file.

 

Policy 2:

{

  "Id": "Policy1500985827175",

  "Version": "2012-10-17",

  "Statement": [

    {

      "Sid": "Stmt1500985820888",

      "Action": [

        "s3:GetObject"

      ],

      "Effect": "Allow",

      "Resource": "arn:aws:s3:::buddybucketpolicy/*",

      "Principal": "*"

    }

  ]

}

 

Description: Allow everyone to access all the files in bucket.

 

Policy 3:

{

  "Id": "Policy1500985827175",

  "Version": "2012-10-17",

  "Statement": [

    {

      "Sid": "Stmt1500985820888",

      "Action": [

        "s3:GetObject"

      ],

      "Effect": "Allow",

      "Resource": "arn:aws:s3:::buddybucketpolicy",

      "Principal": "*"

    }

  ]

}

 

Description: Invalid Policy: Action does not apply to any resource(s) in statement.

 

Policy 4:

{

  "Id": "Policy1500986383198",

  "Version": "2012-10-17",

  "Statement": [

    {

      "Sid": "Stmt1500986379681",

      "Action": [

        "s3:GetObject"

      ],

      "Effect": "Allow",

      "Resource": "arn:aws:s3:::buddybucketpolicy/*",

      "Principal": {

        "AWS": [

          "arn:aws:iam::123456789012:user/heartinbt"

        ]

      }

    }

  ]

}

Description: Access to user heartinbt in account 123456789012.

 

Policy 5:

{

  "Id": "Policy1500986383198",

  "Version": "2012-10-17",

  "Statement": [

    {

      "Sid": "Stmt1500986379681",

      "Action": [

        "s3:GetObject"

      ],

      "Effect": "Allow",

      "Resource": "arn:aws:s3:::buddybucketpolicy/*",

      "Principal": {

        "AWS": [

          "123456789012"

        ]

      }

    }

  ]

}

 

Description:  Access to user heartinbt in account 123456789012

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream