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.
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.
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.
As you can see current status is Disabled
Let’s edit it to enable versioing on this bucket
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.
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 🙂
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 versioning–demo-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:
- Create a DynamoDB table using CloudFormation
- 5 Ways to Create and Manage your AWS Resources
- AWS S3 Storage Classes: Everything you need to know
- How to Create an EC2 instance in an existing VPC using CloudFormation
- Attach an IAM role to an EC2 instance using CloudFormation
- How to create an S3 bucket using CloudFormation
- Understand IAM PassRole to Secure Your AWS Infrastructure
- Understand IAM PassRole to Secure your AWS Infrastructure
- Serverless Services on AWS with Explanation