Automating Cloud Storage Lifecycle Policies with Terraform

Managing storage costs and compliance manually is tedious. Terraform lets you define and apply lifecycle policies—like moving objects to colder storage or deleting old versions—as code across AWS S3, GCS, or Azure Blob Storage. This guide walks you through the essentials.

First, ensure you have Terraform installed and your cloud provider provider configured. For example, with AWS S3, declare the terraform block and AWS provider. Then define a bucket resource. The key is the lifecycle_rule block inside the resource.

Article illustration

Defining Rule Actions and Transitions

Each lifecycle rule contains one or more actions. Use transition to move objects to cheaper storage classes (e.g., Standard-IA after 30 days, Glacier after 90).

  • Transition: days (integer) and storage_class (string).
  • Expiration: expired_object_delete_marker or days to delete objects.
  • Noncurrent version expiration: for versioned buckets, use noncurrent_version_expiration.

Example S3 Lifecycle Rule Block

lifecycle_rule {
  id      = "archive"
  enabled = true
  prefix  = "logs/"
  transition {
    days          = 30
    storage_class = "STANDARD_IA"
  }
  expiration {
    days = 365
  }
}

Applying Rules to Multiple Resources with Modules

To avoid repetition, wrap lifecycle rules in a module. Pass variables like prefix, transition_days, and expiration_days. This makes your Terraform code DRY and reusable for different buckets or environments.

Testing and Applying Changes

Run terraform plan to review rule changes. Then terraform apply. Terraform will update the lifecycle policies on the existing bucket—no manual steps. For complex policies, use condition and filter blocks to target specific tags or object sizes.

Remember to check your cloud provider’s pricing: lifecycle transitions incur per-object costs.

Starting with a simple rule and gradually adding conditions is the safest approach. Terraform’s declarative syntax makes lifecycle management predictable and auditable, saving you from accidental data loss or runaway storage costs.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *