Archiving data to GCP Storage Bucket with lifecycle policies
TypeScriptCreating a Google Cloud Storage Bucket with lifecycle policies to manage object archiving and deletion involves defining rules that automatically perform actions on objects within the bucket based on criteria like object age, storage class transitions, and other conditions.
In the case of implementing archiving and lifecycle policies, you can use the
gcp.storage.Bucket
resource, which allows you to provision a new storage bucket and apply lifecycle management policies. ThelifecycleRules
property is used to specify these policies. Each rule consists of anaction
, such as "Delete" or "SetStorageClass", and acondition
, which indicates when the action should be applied (e.g., age of the object).Here's a Pulumi program in TypeScript demonstrating how you might create a Google Cloud Storage Bucket with a lifecycle policy that archives objects to a colder storage class after 30 days, then deletes them after a year:
import * as pulumi from "@pulumi/pulumi"; import * as gcp from "@pulumi/gcp"; // Create a GCP Storage Bucket const bucket = new gcp.storage.Bucket("my-archive-bucket", { location: "US", storageClass: "STANDARD", // Starting storage class lifecycleRules: [ { // Transition to NEARLINE storage after 30 days action: { type: "SetStorageClass", storageClass: "NEARLINE" }, condition: { age: 30, // Number of days old }, }, { // Delete objects after 365 days action: { type: "Delete", }, condition: { age: 365, // Number of days old }, }, ], }); // Export the bucket's name and URL export const bucketName = bucket.name; export const bucketUrl = pulumi.interpolate`gs://${bucket.name}`;
Explanation:
- We import the necessary Pulumi packages.
- We create a new storage bucket with the
gcp.storage.Bucket
resource (documentation). - We set the initial storage class to "STANDARD," which is the default for frequently accessed data.
- We define a lifecycle rule that changes the storage class of objects in the bucket to "NEARLINE" after they are 30 days old. "NEARLINE" is a cost-effective storage class for data that is accessed less frequently.
- We add another lifecycle rule to delete objects that are 365 days old.
- Finally, we export the bucket name and URL, which can be used to interact with the bucket in future operations.
This Pulumi code will provision the described resources in Google Cloud when executed with the Pulumi CLI and set up the proper authentication. The resources can then be managed, updated, or destroyed through further Pulumi commands.