Sub topic 12
Automating transitions and expiration with lifecycle rules
This chapter assumes you can create a bucket, know what object keys and prefixes are, and understand the S3 storage classes and versioning from earlier chapters. A lifecycle configuration is a set of rules attached to a bucket. S3 applies the rules for you, so you don't have to write scripts that move or delete objects.
What a rule contains
Each rule has four parts:
- ID: a name you choose.
- Status:
EnabledorDisabled. - Filter: which objects the rule applies to. You can filter by key prefix, by object tags, by object size, or by a combination of these. An empty filter matches every object.
- Actions: what S3 does when the conditions are met.
The two common actions are a transition, which moves an object to another storage class after a number of days, and an expiration, which deletes the object after a number of days. Days are counted from the object's creation date.
A worked example
The configuration below applies to objects under logs/. It moves them to Standard-IA after 30 days and to Glacier Flexible Retrieval after 90 days. It deletes them after 365 days.
{
"Rules": [
{
"ID": "archive-then-expire-logs",
"Status": "Enabled",
"Filter": { "Prefix": "logs/" },
"Transitions": [
{ "Days": 30, "StorageClass": "STANDARD_IA" },
{ "Days": 90, "StorageClass": "GLACIER" }
],
"Expiration": { "Days": 365 }
}
]
}Save it as lifecycle.json and apply it:
aws s3api put-bucket-lifecycle-configuration \
--bucket my-example-bucket-123456789012 \
--lifecycle-configuration file://lifecycle.jsonThis call replaces the bucket's whole lifecycle configuration. If the bucket already has rules, fetch them with get-bucket-lifecycle-configuration and include them in your file.
Rules for transitions and common errors
Transitions follow some constraints that cause most failed configurations:
- Objects move down the storage class ladder only. You cannot use a rule to move an object from Glacier back to Standard.
- When you move objects to Standard-IA, the objects must be stored for at least 30 days in the Standard class first. Later transitions must be at least 30 days after the earlier one in the same rule.
- Small objects are often not worth moving. Standard-IA and some other classes bill a minimum object size (128 KB), so moving many tiny objects can cost more than it saves. You can set a minimum size in the filter to skip them.
- Each class has a minimum storage duration. Deleting or moving an object early still bills you for the remaining days.
- Transitions cost a per-request fee. For millions of small objects, estimate the request charges before you enable the rule.
Lifecycle rules with versioning
When versioning is on, expiration behaves differently:
- For a current version,
Expirationdoes not remove data. S3 adds a delete marker, and the object becomes a noncurrent version. - To clean up old versions, use
NoncurrentVersionTransitionsandNoncurrentVersionExpiration. Their day counts start when the version became noncurrent. - A delete marker with no remaining versions is an expired delete marker. Use
ExpiredObjectDeleteMarkerto remove these, or they accumulate.
Here is a rule that removes noncurrent versions after 60 days and cleans up delete markers:
{
"Rules": [
{
"ID": "clean-old-versions",
"Status": "Enabled",
"Filter": {},
"NoncurrentVersionExpiration": { "NoncurrentDays": 60 },
"Expiration": { "ExpiredObjectDeleteMarker": true }
}
]
}Cleaning up incomplete multipart uploads
Parts of an unfinished multipart upload stay in the bucket and are billed. Add an AbortIncompleteMultipartUpload action with DaysAfterInitiation to remove them. The rule needs a filter, and an empty one applies to the whole bucket.
Timing
S3 evaluates lifecycle rules once a day, and actions can run some time after the day count is reached. Do not rely on exact timing. S3 rounds the expiry time up to midnight UTC, and you aren't billed for storage once an object is eligible for expiration.
Exercise
A bucket has versioning enabled. A lifecycle rule has Expiration set to 30 days for the prefix tmp/. After 30 days, you still see storage charges for those objects. Why, and which rule element would remove the data?
Show answer
On a versioned bucket, expiring the current version only adds a delete marker, and the object becomes a noncurrent version that is still stored and billed. Add NoncurrentVersionExpiration (for example, NoncurrentDays set to 1 or more) to delete the old versions, and ExpiredObjectDeleteMarker to remove the leftover delete marker.