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β
| Name | Type | Description |
|---|---|---|
| region | string | AWS region where the target S3 bucket resides |
| bucket_name | string | The name of the versioned S3 bucket |
| noncurrent_days | number | Number of days to retain noncurrent object versions before deletion |
| prefix_filter | string | (Optional) Prefix to target specific objects (e.g., "logs/") |
| environment | string | Tag to specify the deployment environment (e.g., dev, staging, prod) |
π€ Outputsβ
| Name | Description |
|---|---|
| bucket_name | The name of the S3 bucket where the rule is applied |
| rule_id | The 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.