S3 Lifecycle Expire
π¦ Overviewβ
This Terraform module applies an Amazon S3 lifecycle rule to automatically expire (delete) objects after a user-defined number of days.
Itβs ideal for managing logs, temporary files, or datasets with strict data retention timelines.
β Key Featuresβ
- Deletes S3 objects automatically after a specified number of days
- Supports optional prefix filtering for targeted expiration
- Enforces data lifecycle compliance and reduces storage costs
- No manual cleanup required after deployment
πΌ Use Casesβ
- Auto-deletion of application logs or debug files
- Cleanup of temporary assets in dev or staging buckets
- Enforcing compliance with retention policies (e.g., 90-day log expiry)
- Lifecycle automation for ephemeral cloud data
π₯ Input Variablesβ
| Name | Type | Description |
|---|---|---|
| region | string | AWS region where the S3 bucket is located |
| bucket_name | string | Name of the existing S3 bucket to apply the rule to |
| expiration_days | number | Number of days after which objects should be expired (deleted) |
| prefix_filter | string | (Optional) Prefix to target specific objects (e.g., "temp/") |
| environment | string | Tag to specify the deployment environment (e.g., dev, staging, prod) |
π€ Outputsβ
| Name | Description |
|---|---|
| bucket_name | The name of the S3 bucket with the lifecycle rule |
| rule_id | The ID of the applied expiration lifecycle rule |
π Deployment Readinessβ
- β Compatible with Terraform 1.0+
- β Tested with AWS provider 5.x
- π§ͺ Requires an existing S3 bucket and S3:PutLifecycleConfiguration permission
π οΈ Example Usageβ
module "s3_lifecycle_expire" {
source = "git::ssh://git@github.com/archiphire/aws-level-1-modules.git//storage/s3-lifecycle-expire?ref=v1.0.0"
region = "us-east-1"
bucket_name = "archiphire-logs"
expiration_days = 90
prefix_filter = "temp/"
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
β οΈ This will remove only the lifecycle expiration rule. The bucket and any existing objects will remain intact unless manually removed.
π‘οΈ Notesβ
This is a Level 1 lifecycle automation module built for modular integration. For enhanced data control and cost optimization, consider pairing with:
s3-lifecycle-to-ia s3-access-logging
π 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.