Sub topic 8
Storage tiering and lifecycle policies
This chapter assumes you know what an S3 bucket and an object are, can run AWS CLI commands, and can read JSON. It covers how to cut storage spend by moving data to cheaper tiers and deleting it when it is no longer needed.
Why storage tiers exist
Storage is billed per gigabyte per month. The price depends on how quickly you need the data back. Data you read often belongs in a fast, more expensive tier. Data you rarely read can sit in a cheaper tier that charges more per retrieval or imposes a minimum storage period.
The common Amazon S3 classes, from hottest to coldest:
| Storage class | Best for | Trade-off |
|---|---|---|
| S3 Standard | Frequently read data | Highest price per GB |
| S3 Standard-IA | Data read less than about once a month | Retrieval fee, 30-day minimum, 128 KB minimum billable size |
| S3 Glacier Instant Retrieval | Rarely read data that still needs millisecond access | Retrieval fee, 90-day minimum |
| S3 Glacier Flexible Retrieval | Archives where waiting minutes to hours is fine | Restore step required before reading |
| S3 Glacier Deep Archive | Long-term retention | Lowest price, restores take hours |
Check current prices on the AWS pricing page, because they change by Region.
Lifecycle policies
A lifecycle policy is a set of rules on a bucket. Each rule selects objects by prefix or tag, then transitions them to another class or expires (deletes) them after a number of days. S3 runs the rules for you, so you don't need scripts.
The rule below moves objects under logs/ to Standard-IA after 30 days and deletes them after 365 days. Save it as lifecycle.json:
{
"Rules": [
{
"ID": "logs-tiering",
"Status": "Enabled",
"Filter": { "Prefix": "logs/" },
"Transitions": [
{ "Days": 30, "StorageClass": "STANDARD_IA" }
],
"Expiration": { "Days": 365 }
}
]
}Apply it to a bucket. Replace my-example-bucket with your bucket name:
aws s3api put-bucket-lifecycle-configuration \
--bucket my-example-bucket \
--lifecycle-configuration file://lifecycle.jsonThis call replaces the bucket's entire existing lifecycle configuration. If the bucket already has rules, fetch them first with aws s3api get-bucket-lifecycle-configuration and merge them into your file.
Common errors and gotchas
- Transitions that cost more than they save. Each transition request has a charge. Millions of tiny objects can cost more to move than you save. Standard-IA also bills a 128 KB minimum per object.
- Minimum storage durations. Deleting or moving an object out of Standard-IA before 30 days still bills for the full 30.
- Unpredictable access. If you can't tell which objects will be read, S3 Intelligent-Tiering moves them between tiers automatically for a small monitoring fee per object.
- Versioned buckets. Expiration on a versioned bucket only adds a delete marker. Add
NoncurrentVersionExpirationto remove old versions, or they keep costing money. - Incomplete multipart uploads. Abandoned uploads store hidden parts that are billed. Add an
AbortIncompleteMultipartUploadaction to clean them up. - Deleting data you need. Expiration is permanent. Confirm retention and compliance requirements with the data owner before enabling it.
Exercises
Exercise
A bucket holds 50 million objects of about 10 KB each. A teammate proposes a rule that moves them all to Standard-IA after 30 days. Why might this raise costs?
Show answer
Standard-IA bills a 128 KB minimum per object, so each 10 KB object is charged as 128 KB. The 50 million transition requests also carry a per-request charge. Together these can exceed the per-GB savings. Combining small objects into larger files first, or using Intelligent-Tiering, is a better option.
Exercise
You apply a lifecycle file to a bucket that already had a rule for another prefix, and that rule stops working. What happened and how do you avoid it?
Show answer
put-bucket-lifecycle-configuration replaces the whole configuration. Retrieve the existing rules with get-bucket-lifecycle-configuration, add the new rule to that list, and apply the merged file.