1. Answers
  2. Setting GCP Cloud Storage lifecycle policies

How do I set GCP Cloud Storage object lifecycle policies?

Introduction

In this example, we will set up object lifecycle policies in GCP Cloud Storage. Lifecycle policies help manage your storage costs by allowing you to control how objects are stored and deleted. You can configure GCP Cloud Storage bucket to automatically transition objects to different storage classes or delete them based on age or other criteria.

Detailed Explanation

We’ll create a Cloud Storage bucket with a lifecycle rule. This rule can perform actions like deleting objects older than a certain number of days or transitioning them to a different storage class. Here’s how you can set it up:

  1. Define a Cloud Storage bucket.
  2. Add a lifecycle rule to delete objects older than 30 days.
  3. Export the bucket name.

Code Example

Below is the Terraform HCL configuration to create a GCP Cloud Storage bucket with a lifecycle policy:

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

const myBucket = new gcp.storage.Bucket("my_bucket", {
    name: "my-unique-bucket-name",
    location: "US",
    lifecycleRules: [{
        action: {
            type: "Delete",
        },
        condition: {
            age: 30,
        },
    }],
});
export const bucketName = myBucket.name;

Explanation of Code

  • Provider Configuration: This section sets the GCP credentials, project, and region.
  • google_storage_bucket Resource: This defines a Cloud Storage bucket with a unique name and location.
  • lifecycle_rule Block: This block outlines the lifecycle rule to delete objects that are 30 days old.
  • Output Block: Finally, this outputs the bucket name.

Key Points

  • Lifecycle policies help manage storage costs effectively by automating object deletion or transition.
  • The google_storage_bucket resource is used to define the bucket.
  • The lifecycle_rule block specifies the actions and conditions for the lifecycle policy.

Summary

We discussed how to set lifecycle policies for a GCP Cloud Storage bucket. The example illustrated how to configure a bucket with a rule to delete objects older than 30 days, helping in efficient storage management.

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