How to Enable Versioning in S3 bucket using Console, CLI and CloudFormation

How to Enable Versioning in S3 bucket using Console, CLI and CloudFormation

Dear Reader, In this post I will help you enable versioning in an S3 Bucket using AWS Management Console, AWS CLI and CoudFormation.

Stay tuned with me to do it in a step by step manner.

What is AWS S3 Versioning?

Versioning is a means of keeping multiple versions of same file in the same S3 bucket. Ideally it lets you store, retrieve and restore each version of every object you store in S3 bucket.

This feature of s3 protects your objects from unintended overwrites and deletes. Therefore, as a security best practice AWS recommends to enable versioning on your buckets.

Alright !!!

We have the basics now, let’s see some of the ways in which we can enable versioning in a s3 bucket-

  • Using AWS Console
  • Using AWS CLI
  • Using CoudFormation

Let’s try all these options one by one.

Okay?

Enable Versioning in S3 Bucket Using Console

In this section we will enable versioning on an S3 bucket from AWS S3 console.

Step 1: Login to AWS Management Console and open S3

Login to AWS Management Console and open S3 service.

You can either go to Services -> Storage -> S3

or

Type s3 in the search bar and hit enter. Once you see S3 option click on that.

How to Create an S3 Bucket 2

Step 2: Choose the bucket on which you want to enable versioning

Once you click on S3, you will see the list of your buckets as you can see below.

How to Enable Versioning in S3 bucket using Console, CLI and CloudFormation 1

Click on your bucket name on which you want to enable versioning.

For this tutorial I will be enabling versioning on cloud-katha bucket using console. For CLI demo, I will create a separate bucket.

Step 3: Go to Properties tab and Edit Versioning Status of Bucket

After you click on your bucket-name, you will see details like below. Click on Properties tab. Once you are there, you will see Bucket Versioning and an Edit button to edit the versioning status of the bucket.

How to Enable Versioning in S3 bucket using Console, CLI and CloudFormation 2

As you can see current status is Disabled

Let’s edit it to enable versioing on this bucket

How to Enable Versioning in S3 bucket using Console, CLI and CloudFormation 3

Select Enable in bucket versioing and click on Save changes

One you click Save changes, versioning is enabled as you can see in below screenshot and is it effect.

Versioing is enabled

Step 4: Verify If Versioning is working fine

You can verify versioning by uploading two objects with same name/key. With versioning enabled, S3 will not override the object rather it will create a new version each time.

Create a text file with name versioning-demo.txt with below content

This is version 1 file

Save the file and upload into cloud-katha bucket

Now update the same text file with below content and upload it again to the same bucket.

This is version 2 file

Let’s observe what happened in console.

If you toggle Show versions button as highlighted below, you can see both the versions of file present in s3. So object was not overwritten instead both the versions were saved, which verifies that versioning is working fine 🙂

How to Enable Versioning in S3 bucket using Console, CLI and CloudFormation vlidate

Enable Versioning in S3 using AWS CLI

This section covers enabling versioning on an S3 bucket using AWS CLI.

Step 1: Create a Bucket

let’s create a bucket with name versioningdemo-cli to demo versioning using CLI

aws s3api create-bucket --bucket versioning-demo-cli

After create, you can do aws s3 ls. You should be able to see your newly created bucket in the list of bucket.

Aright, let’s go ahead and enable versioning on this bucket.

Step 2: Enable Versioning

The syntax for versioning is as below

aws s3api put-bucket-versioning --bucket bucketnamehere --versioning-configuration MFADelete=Disabled,Status=Enabled

So after substituting bucket name command is like-

aws s3api put-bucket-versioning --bucket versioning-demo-cli --versioning-configuration MFADelete=Disabled,Status=Enabled

–versioning-configuration is the container structure for setting the versioning state of a bucket. It is represented as below in JSON

{
  "MFADelete": "Enabled"|"Disabled",
  "Status": "Enabled"|"Suspended"
}

However, there is a shorthand syntax accepted as well which is

MFADelete=Disabled/Enabled,Status=Enabled/Suspended

Once you enter the command, versioning gets enabled so lets check the versioning status of our bucket.

Step 3: Check versioning status of versioning-demo-cli bucket

aws s3api get-bucket-versioning --bucket versioning-demo-cli

Response:

{
    "Status": "Enabled",
    "MFADelete": "Disabled"
}

Which means versioning is enabled on bucket.

Enable Versioning in S3 using CloudFormation

Enabling versioning on an s3 bucket using CloudFormation is easy. All you need is to add a VersioningConfiguration parameter and make status as enabled as shown below.

Resources:
  S3Bucket:
    Type: AWS::S3::Bucket
    Description: Creating Amazon S3 bucket from CloudFormation
    Properties:
      BucketName: i-named-this-bucket
      VersioningConfiguration:
        Status: Enabled

If you are looking for a step by step tutorial to create an s3 bucket with versioning using CoudFormation, feel free to checkout my below post.

Conclusion

In this post, we learnt how to enable versioning in an S3 Bucket using AWS Management Console, AWS CLI and CoudFormation.

I hope you were able to work up with me and able to enable versioning in your S3 Bucket. If you get stuck at any time feel free to add a comment. I will reply to your query asap.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

If you liked reading my post, you can motivate me by-

  • Adding a comment below on what you liked and what can be improved.
  • Follow us on 
  • Share this post with your friends and colleagues.

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *