Skip to main content

S3 Lifecycle Noncurrent Cleanup

πŸ“¦ Overview​

This Terraform module applies a lifecycle rule to an existing Amazon S3 bucket that automatically deletes noncurrent (previous) versions of objects after a specified number of days.

It’s ideal for versioned S3 buckets that require storage cost control and routine cleanup of outdated object versions.


βœ… Key Features​

  • Deletes noncurrent object versions after a defined retention period
  • Supports optional prefix filtering to target specific object paths
  • Helps manage costs in versioned buckets with frequent updates
  • Safe to use in production with granular rule configuration

πŸ’Ό Use Cases​

  • Managing object version sprawl in large S3 buckets
  • Reducing cost in environments with frequent object overwrites
  • Cleaning up logs or staging data with frequent version churn
  • Applying fine-grained S3 lifecycle controls alongside versioning

πŸ“₯ Input Variables​

NameTypeDescription
regionstringAWS region where the target S3 bucket resides
bucket_namestringThe name of the versioned S3 bucket
noncurrent_daysnumberNumber of days to retain noncurrent object versions before deletion
prefix_filterstring(Optional) Prefix to target specific objects (e.g., "logs/")
environmentstringTag to specify the deployment environment (e.g., dev, staging, prod)

πŸ“€ Outputs​

NameDescription
bucket_nameThe name of the S3 bucket where the rule is applied
rule_idThe ID of the applied noncurrent version rule

πŸš€ Deployment Readiness​

  • βœ… Compatible with Terraform 1.0+
  • βœ… Tested with AWS provider 5.x
  • πŸ§ͺ Requires that versioning is already enabled on the S3 bucket

πŸ› οΈ Example Usage​

module "s3_lifecycle_noncurrent_cleanup" {
source = "git::ssh://git@github.com/archiphire/aws-level-1-modules.git//storage/s3-lifecycle-noncurrent-cleanup?ref=v1.0.0"

region = "us-east-1"
bucket_name = "my-versioned-bucket"
noncurrent_days = 30
prefix_filter = null
environment = "prod"
}
terraform init
terraform plan
terraform apply

or

tofu init
tofu plan
tofu apply

🧹 Cleanup Options​

Terraform Destroy (for test environments):

terraform destroy

or

tofu destroy

⚠️ Only run terraform destroy if this module was deployed independently. If used in a broader infrastructure stack, consider manual cleanup of the rule.

πŸ›‘οΈ Notes​

This is a Level 1 baseline module designed for modular lifecycle automation. It assumes S3 versioning is already enabled.

Use alongside:

s3-lifecycle-to-ia s3-lifecycle-expire s3-secure-bucket

For a full S3 lifecycle and retention policy.

πŸ”’ Deployment Package Notice​

This module is part of the Level 1 AWS License Tier. To access the full deployment package and source code, subscribe to Archiphire.