1. Answers
  2. Provision an AWS S3 Bucket Lifecycle Configuration

How do I provision an AWS S3 Bucket lifecycle configuration?

To manage the lifecycle of objects in an S3 bucket, we can set rules to transition objects to cheaper storage classes, archive them, or delete them after a specific period. We’ll create an S3 bucket and define a lifecycle rule which transitions objects to the Standard-IA storage class 30 days after creation and then to Glacier storage class 90 days after creation.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an S3 bucket
const myBucket = new aws.s3.BucketV2("my_bucket", {
    bucket: "my-unique-bucket-name",
    acl: "private",
});
// Set up a lifecycle rule for the bucket
const myBucketLifecycle = new aws.s3.BucketLifecycleConfigurationV2("my_bucket_lifecycle", {
    bucket: myBucket.bucket,
    rules: [{
        id: "transition_rule",
        prefix: "",
        status: "Enabled",
        transitions: [
            {
                days: 30,
                storageClass: "STANDARD_IA",
            },
            {
                days: 90,
                storageClass: "GLACIER",
            },
        ],
        expiration: {
            days: 365,
        },
    }],
});
export const bucketName = myBucket.bucket;
export const lifecycleRuleId = myBucketLifecycle.rules.apply(rules => rules[0].id);

In this example, an AWS S3 bucket is created. A lifecycle rule is then applied to this bucket which transitions objects to the STANDARD_IA storage class after 30 days, and to GLACIER after 90 days. Finally, objects expire and are deleted after 365 days. This setup helps in optimizing costs effectively by automatically managing the storage class of the objects based on their age. The outputs provide the bucket name and the ID of the lifecycle rule for reference.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up