How to create a lifecycle policy for an S3 Bucket on AWS
We can use the Lifecycle Policy to manage the objects in S3 Bucket so that they are stored cost-effectively throughout. An S3 Lifecycle Policy is a set of rules used to define actions that Amazon S3 applies to objects in the bucket. There are two types of actions:
- Transition actions:
This is used to define when objects transition to another storage class.
- Expiration actions:
This is used to define when objects expire.
We can add rules in the policy to tell Amazon S3 to transition objects to another Amazon S3 storage class.
- When we know that objects are infrequently accessed, we might transition them to the S3 Standard-IA storage class.
- We might want to archive objects that we don't need to access in real-time to the S3 Glacier storage class.
To know more about the transition in detail, click here to visit the official documentation.
We can use the S3 Lifecycle Policy in the following cases.
- When we want to upload logs to a bucket needed for a week or a month and after that, we might want to delete them.
- When documents are frequently accessed for a limited period of time.
- When we want to upload some types of data to Amazon S3 primarily for archival purposes.
We can add S3 Lifecycle configurations to unversioned buckets and versioning-enabled buckets.
In this article, we will see the steps to move objects (Transition Action) to Glacier after 10 days of creation and delete(Expirations Action) them after 365 days of creation.
What will we do?
- Login to AWS.
- Create a Lifecycle Policy
Login to AWS
Click here to go to AWS Login Page.
When we hit the above link, we will see a web page as follows where we are required to login using our login details.
Once you log in, you see the AWS Management Console as follows.
Create a Lifecycle Policy
Click on "Services" in the top left of the screen and then search for s3 in the search box.
You will see the main dashboard of the S3 where you will see all the S3 buckets from your account. Click on your S3 bucket in which you want to create a lifecycle policy. If you do not have an S3 bucket and want to learn to create your first bucket then click here.
Note: While creating an S3 Bucket make sure that the name you give to the Bucket is unique globally as no 2 buckets can have the same name. You can not have buckets with the same name not only in your account but throughout the globe.
Inside the S3 Bucket, click in the Management tab.
You will see a screen as follows, specify a name for the policy to be created and choose the scope as "Apply to all objects in the bucket" if you want to apply this policy on all the objects in the bucket.
Choose the object version on which you want to apply the lifecycle/transition policy. Here I have chosen the current version.
Here I have chosen the transition to Glacier and specified 10 days as the number of days after the creation of the object that I want the transition to be applied.
Click on the acknowledgment checkbox and then on Next.
Here I have chosen the current version of the object for expiration and specified 365 days as the number of days after object creation to delete the object.
If you want to try to create a policy for the previous version of documents as well, check on the "Previous versions" checkbox.
Click on the Next Button.
Verify the settings for the rule we created if you need to make changes, choose Previous. Otherwise, click on the acknowledgment checkbox and then on the Save button.
Here, you can see the rule we created that will move the current version of the objects to Glacier after 10 days of creation and will delete the current version of the objects after 365 days of creation.
In this article, we saw the step to create a lifecycle policy on s3 buckets to move the current version of the objects to Glacier and delete the current version of the objects from the bucket.