Setting Cloud Storage Lifecycle Rules

Hands-On Lab

 

Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content

Length

00:30:00

Difficulty

Beginner

While saving an object in a Cloud Storage bucket is relatively inexpensive, there is, nonetheless, a cost. The cost varies depending on the storage class selected. Certain objects are required to be more available at first, requiring the storage class with the highest availability — and cost. Such objects may eventually be relegated to less available and less expensive storage classes and, even, be deleted. Management of these objects over time can be handled automatically by establishing and implementing lifecycle rules. In this hands-on lab, we'll set a variety of lifecycle rules for Google Cloud Storage buckets both from the console and the command line.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Setting Cloud Storage Lifecycle Rules

In this hands-on lab, we'll set a variety of lifecycle rules for Google Cloud Storage buckets both from the console and the command line.

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says "Open Link in Incognito Window"). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.

On the Welcome to your new account screen, review the text, and click Accept. In the "Welcome L.A.!" pop-up once you're signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

Setting Cloud Storage Lifecycle Rules

Now, on to the lab!

Create a Cloud Storage bucket.

  1. From the Google Cloud console main navigation, navigate to Storage > Browser.
  2. Click Create bucket.
  3. Name the bucket uniquely (e.g., "la-lifecycle-" with random numbers at the end).
  4. In the Default storage class section, select Regional.
  5. Click Create.

Define first lifecycle rule.

  1. From the Cloud Storage browser page, click None in the Lifecycle column for the bucket just created.
  2. Click Add rule.
  3. Under Select object conditions, set the following:
    • Age: 180
    • Storage class: Regional, Standard
  4. Click Continue.
  5. Under Select action, choose Set to Nearline.
  6. Click Continue.
  7. Click Save.

Define second lifecycle rule.

  1. From the Cloud Storage browser page, click Enabled in the Lifecycle column.
  2. Click Add rule.
  3. Under Select object conditions, set the following:
    • Age: 365
    • Storage class: Nearline
  4. Click Continue.
  5. Under Select action, choose Set to Coldline.
  6. Click Continue.
  7. Click Save.

From command line, get lifecycle rules.

  1. Click the icon in the top navigation to activate Cloud Shell.

  2. In the Cloud Shell, enter the following code:

    gsutil lifecycle get gs://<BUCKET_NAME>
  3. Review the output.

Set lifecycle rule with JSON file.

  1. Clone the course repo:

    git clone https://github.com/linuxacademy/content-gc-essentials
  2. Change to the lab's directory:

    cd cloud-storage-lifecycle-lab
  3. Click the pencil icon to launch the Cloud Shell Editor.

  4. In the code editor, open the cloud-storage-lifecycle-lab and select the delete-after-two-years.json file to review it.

  5. In the Cloud Shell, enter the following code to set the lifecycle rules using the JSON file:

    gsutil lifecycle set delete-after-two-years.json gs://<BUCKET_NAME>
  6. Confirm the lifecycle rule has been added in the console.

Conclusion

Congratulations on completing this lab!