Last updated: Jan 26, 2024
Reading timeยท4 min
S3 lifecycle rules enable us to transition the objects we store to different categories in an attempt to lower costs.
In order to add lifecycle rules to an S3 bucket in AWS CDK, we have to provide the lifecycleRules prop when instantiating the Bucket class.
Let's look at an example of adding a lifecycle rule with multiple transitions to an S3 bucket.
import * as s3 from 'aws-cdk-lib/aws-s3'; import * as cdk from 'aws-cdk-lib'; export class CdkStarterStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); const s3Bucket = new s3.Bucket(this, 's3-bucket', { removalPolicy: cdk.RemovalPolicy.DESTROY, autoDeleteObjects: true, // ๐ set up lifecycle rules lifecycleRules: [ { // ๐ optionally apply object name filtering // prefix: 'data/', abortIncompleteMultipartUploadAfter: cdk.Duration.days(90), expiration: cdk.Duration.days(365), transitions: [ { storageClass: s3.StorageClass.INFREQUENT_ACCESS, transitionAfter: cdk.Duration.days(30), }, { storageClass: s3.StorageClass.INTELLIGENT_TIERING, transitionAfter: cdk.Duration.days(60), }, { storageClass: s3.StorageClass.GLACIER, transitionAfter: cdk.Duration.days(90), }, { storageClass: s3.StorageClass.DEEP_ARCHIVE, transitionAfter: cdk.Duration.days(180), }, ], }, ], }); } }
Let's go over what we did in the code sample.
We created an S3 bucket, to which we provided some clean-up props like
removalPolicy
and autoDeleteObjects
, which will take care of emptying and
deleting the bucket when we delete the CDK stack.
We set the lifecycleRules
prop when instantiating the Bucket
class. Some
of the configuration properties a lifecycle rule consists of are:
prefix
- apply the rule only to objects that match the prefix, i.e.
only objects with a prefix of logs/
.abortIncompleteMultipartUploadAfter
- delete incomplete multipart uploads
after a number of days, in our case - after 90 daysexpiration
- delete files from S3 and Glacier after a specified number of
days, in our case - after 365 daystransitions
- An array of transition rules that specify after how many
days the object should be transitioned to a different storage tierThe transition rules we've set end up looking like this:
Day | Action |
---|---|
Day 0 | Objects were uploaded to the bucket |
Day 30 | Objects transition to standard Infrequent Access |
Day 60 | Objects transition to Intelligent Tiering |
Day 90 | Objects transition to Glacier |
Day 180 | Objects transition to Glacier Deep Archive |
Day 365 | Objects expire and get deleted from S3 and Glacier |
There are a couple of things we should keep in mind when configuring S3 lifecycle rules in AWS CDK.
The transition to INTELLIGENT_TIERING
must come at least 30 days after the
transition to INFREQUENT_ACCESS
, if both are set.
The transition to GLACIER
must come at least 30 days after the transition to
INTELLIGENT_TIERING
.
DEEP_ARCHIVE
must come at least 90 days after the transition to GLACIER
. That's because if we store data in Glacier for less than 90 days, we end up paying for the whole 90 days.Let's run the deploy
command.
npx aws-cdk deploy
If we look at the S3 management console, we can see that the lifecycle rule has been created. It applies to the entire bucket because we didn't specify an object prefix.
Now that the lifecycle rule has been applied to the S3 bucket, all objects we upload would follow the transitions from the table.
To add a lifecycle rule to an S3 bucket after it has been created, use the addLifecycleRule method on the bucket instance.
Let's add a lifecycle rule to our bucket that only applies to objects with a
prefix of logs/
.
import * as s3 from 'aws-cdk-lib/aws-s3'; import * as cdk from 'aws-cdk-lib'; export class CdkStarterStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); // ... rest // ๐ add a life cycle rule after bucket creation s3Bucket.addLifecycleRule({ prefix: 'logs/', expiration: cdk.Duration.days(90), transitions: [ { storageClass: s3.StorageClass.ONE_ZONE_INFREQUENT_ACCESS, transitionAfter: cdk.Duration.days(60), }, ], }); } }
We specified a prefix
of logs/
. This means that the lifecycle rule we
provided will only be applied if the object has that prefix.
Let's run the deploy
command.
npx aws-cdk deploy
After a successful deployment, we can see that a second lifecycle rule has been
created, which only applies to objects with a prefix of logs/
.
The transition rules for our S3 Bucket, for objects with a prefix of logs/
,
end up looking like this:
Day | Action |
---|---|
Day 0 | Objects uploaded |
Day 60 | Objects transition to One Zone Infrequent Access |
Day 90 | Objects expire and get deleted from s3 and glacier |
To delete the resources we have provisioned, issue the destroy
command:
npx aws-cdk destroy
You can learn more about the related topics by checking out the following tutorials: