S3 Bucket Lifecycle in AWS
S3 Bucket Lifecycle is a feature in Amazon S3 that allows you to automate the management of objects in your buckets. Using lifecycle rules, you can automatically transition objects between storage classes, archive them, or delete them after a specified period. This helps optimize storage costs and manage data retention policies efficiently.
Key Features of S3 Lifecycle
- Transition Actions: Automatically move objects to a different storage class (e.g., from S3 Standard to S3 Standard-IA, Glacier, or Glacier Deep Archive) after a certain number of days.
- Expiration Actions: Automatically delete objects after a specified period.
- Noncurrent Version Actions: Manage previous versions of objects if versioning is enabled, such as transitioning or expiring noncurrent versions.
Example Scenario
Suppose you have a bucket named company-data where you store daily log files. You want to:
- Keep logs in S3 Standard for 30 days (for quick access).
- Move logs to S3 Standard-IA after 30 days (for cost savings).
- Move logs to S3 Glacier Deep Archive after 180 days (for long-term retention).
- Delete logs after 365 days.
Example: Lifecycle Policy JSON
Here is a sample lifecycle configuration for this scenario:
{
"Rules": [
{
"ID": "LogLifecycleRule",
"Prefix": "logs/",
"Status": "Enabled",
"Transitions": [
{
"Days": 30,
"StorageClass": "STANDARD_IA"
},
{
"Days": 180,
"StorageClass": "DEEP_ARCHIVE"
}
],
"Expiration": {
"Days": 365
}
}
]
}
How to Apply the Lifecycle Policy (AWS CLI)
- Save the above JSON to a file, e.g.,
lifecycle.json. - Apply the policy to your bucket:
aws s3api put-bucket-lifecycle-configuration --bucket company-data --lifecycle-configuration file://lifecycle.json
What Happens Next
- New log files uploaded to the
logs/prefix remain in S3 Standard for 30 days. - After 30 days, they are automatically transitioned to S3 Standard-IA.
- After 180 days, they are moved to S3 Glacier Deep Archive.
- After 365 days, they are automatically deleted from the bucket.
Benefits
- Cost Optimization: Automatically move data to lower-cost storage classes as it ages.
- Data Management: Enforce data retention policies without manual intervention.
- Compliance: Ensure data is retained or deleted according to regulatory requirements.
