Skip to content

How to enable versioning for an S3 bucket using CloudFormation?

1

I created an S3 bucket using this CloudFormation template. Resources:
MyBucket:
Type: AWS::S3::Bucket

Now, I want to enable versioning for the bucket. How can I modify my template to achieve this? Additionally, do I need to add any IAM policies for users to access the versioned objects?

3 Answers
2
Accepted Answer

Hi Kazi,

You can enable S3 versioning on a bucket through CloudFormation by using the VersioningConfiguration property with value Enabled on your S3 Bucket resource:

VersioningConfiguration:
  Status: Enabled

After updating your template you can create a CloudFormation changeset to update an existing (deployed) Stack. There are multiple ways to do this, one way is to select your CloudFormations stack from the Console, and in the Stack actions drop down click Create changeset for this current stack. You can then select your updated template, create and then execute your changeset. This will modify existing resources with the new template.

For your IAM policy question, there are number of versioned actions that work both on the Object and Bucket level that I have identified through the IAM console by creating a policy and searching for "Version" in the Actions step. They are:

  • s3:DeleteObjectVersion
  • s3:GetObjectVersionTagging
  • s3:ListBucketVersions
  • s3:PutObjectVersionTagging
  • s3:DeleteObjectVersionTagging
  • s3:GetBucketVersioning
  • s3:GetObjectVersionAttributes
  • s3:GetObjectVersionTorrent
  • s3:PutObjectVersionAcl
  • s3:GetObjectVersionAcl
  • s3:GetObjectVersionForReplication
  • s3:PutBucketVersioning
  • s3:GetObjectVersion

It is best practice to only include the actions you need, you can review what each one does from the documentation. To address your error on the other answer, some of these actions apply on an S3 Object, and others on the Bucket level. You can add both to your IAM policy as follows, replace {BUCKET_NAME} with your bucket name:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:DeleteObjectVersion",
                "s3:GetObjectVersionTagging",
                "s3:ListBucketVersions",
                "s3:PutObjectVersionTagging",
                "s3:DeleteObjectVersionTagging",
                "s3:GetBucketVersioning",
                "s3:GetObjectVersionAttributes",
                "s3:GetObjectVersionTorrent",
                "s3:PutObjectVersionAcl",
                "s3:GetObjectVersionAcl",
                "s3:GetObjectVersionForReplication",
                "s3:PutBucketVersioning",
                "s3:GetObjectVersion"
            ],
            "Resource": [
                "arn:aws:s3:::{BUCKET_NAME}/*",
                "arn:aws:s3:::{BUCKET_NAME}"
            ]
        }
    ]
}

I hope this helps.

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Thanks, Gary for coming up with a detailed input. I shall try your way as well.

1

Thanks for the rectification suggestions. I appreciate your input!

answered 2 years ago

0

I did some research and found a way out. To enable versioning for the S3 bucket in the CloudFormation template, it's possible to modify it like this:Resources:

MyBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled

IAM Policy Suggestion: To allow users to access versioned objects, include s3:GetObjectVersion and s3:ListBucketVersions in IAM policy. Here's an example:

{ "Effect": "Allow", "Action": [ "s3:GetObject", "s3:GetObjectVersion", "s3:ListBucket", "s3:ListBucketVersions" ],

"Resource": "arn:aws:s3:::mybucket" }

This ensures both bucket and version-specific permissions are granted.

answered 2 years ago

EXPERT

reviewed 2 years ago

  • There's an error in the policy example. The s3:ListBucket and s3:ListBucketVersions permissions will never match the resource specification "arn:aws:s3:::mybucket/*", because the permissions only apply to a bucket, while the /* suffix only applies to objects in the bucket. For the bucket-level permissions, the resource specification must be "arn:aws:s3:::mybucket".

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.