How to Set Lifecycle Rule in AWS S3: Simple Guide
To set a lifecycle rule in AWS S3, use the
LifecycleConfiguration property in your bucket settings to define rules that automate actions like transitioning objects to cheaper storage or deleting them after a set time. You can configure these rules via the AWS Management Console, AWS CLI, or Infrastructure as Code tools like CloudFormation or Terraform.Syntax
The lifecycle rule configuration in AWS S3 uses a JSON or XML structure defining Rules. Each rule includes an ID, Status (Enabled or Disabled), Filter to select objects, and Actions like Transition or Expiration.
Key parts:
ID: A unique name for the rule.Status: Enables or disables the rule.Filter: Defines which objects the rule applies to (prefix or tags).Transition: Moves objects to another storage class after days.Expiration: Deletes objects after a set number of days.
json
{
"Rules": [
{
"ID": "string",
"Status": "Enabled|Disabled",
"Filter": {
"Prefix": "string"
},
"Transitions": [
{
"Days": integer,
"StorageClass": "STANDARD_IA|GLACIER|DEEP_ARCHIVE"
}
],
"Expiration": {
"Days": integer
}
}
]
}Example
This example shows a lifecycle rule that moves objects with prefix logs/ to the STANDARD_IA storage class after 30 days and deletes them after 365 days.
json
{
"Rules": [
{
"ID": "MoveLogsToIAAndExpire",
"Status": "Enabled",
"Filter": {
"Prefix": "logs/"
},
"Transitions": [
{
"Days": 30,
"StorageClass": "STANDARD_IA"
}
],
"Expiration": {
"Days": 365
}
}
]
}Output
Lifecycle rule applied: Objects with prefix 'logs/' transition to STANDARD_IA after 30 days and expire after 365 days.
Common Pitfalls
Common mistakes when setting lifecycle rules include:
- Not enabling the rule (
Statusmust beEnabled). - Incorrect
PrefixorFiltercausing rules to not apply. - Setting conflicting rules that overlap on the same objects.
- Forgetting that lifecycle actions run once a day, so changes are not immediate.
Always validate your JSON and test with a small bucket before applying to production.
json
Wrong example (rule disabled):
{
"Rules": [
{
"ID": "ExpireOldFiles",
"Status": "Disabled",
"Filter": {"Prefix": "old/"},
"Expiration": {"Days": 90}
}
]
}
Right example (enabled rule):
{
"Rules": [
{
"ID": "ExpireOldFiles",
"Status": "Enabled",
"Filter": {"Prefix": "old/"},
"Expiration": {"Days": 90}
}
]
}Quick Reference
Summary tips for S3 lifecycle rules:
- Use
IDto name rules clearly. - Set
StatustoEnabledto activate rules. - Use
Filterto target specific objects by prefix or tags. - Use
Transitionsto save costs by moving objects to cheaper storage. - Use
Expirationto delete objects automatically and save space.
Key Takeaways
Enable lifecycle rules with clear IDs and proper filters to automate object management.
Use transitions to move objects to cheaper storage classes after a set time.
Set expiration to delete objects automatically and reduce storage costs.
Validate your lifecycle configuration JSON and test on non-critical buckets first.
Remember lifecycle actions run once daily, so changes are not immediate.