Skip to main content

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​

NameTypeDescription
regionstringAWS region where the S3 bucket is located
bucket_namestringName of the existing S3 bucket to apply the rule to
expiration_daysnumberNumber of days after which objects should be expired (deleted)
prefix_filterstring(Optional) Prefix to target specific objects (e.g., "temp/")
environmentstringTag to specify the deployment environment (e.g., dev, staging, prod)

πŸ“€ Outputs​

NameDescription
bucket_nameThe name of the S3 bucket with the lifecycle rule
rule_idThe 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.