Store objects in three S3 storage classes and add a lifecycle configuration
Task
Create a private Amazon S3 bucket, confirm that Block Public Access is on, and upload three small objects you write yourself, each in a different storage class: S3 Standard, S3 Standard-IA and S3 Intelligent-Tiering. Then add an S3 Lifecycle configuration with two rules: one that moves log objects to cheaper classes as they age and finally deletes them, and one that cleans up unfinished multipart uploads. At the end you can show where each object's storage class is recorded, and read back the rules S3 would apply by age without anyone having to remember.
Steps
- Choose the bucket name. A bucket name must be unique across all of AWS, so it ends in your account ID. Replace
111122223333with your own:
aws sts get-caller-identity --query Account --output text
BUCKET=clf-lab18-storage-111122223333
echo $BUCKET
- Create the bucket in your CloudShell Region, then confirm that Block Public Access is on:
aws s3 mb s3://$BUCKET
aws s3api get-public-access-block --bucket $BUCKET
All four settings, BlockPublicAcls, IgnorePublicAcls, BlockPublicPolicy and RestrictPublicBuckets, read true. Leave them so. 3. Write three short text files of invented content, and upload each in a different storage class:
echo "INFO sample application log line for a lab" > app-1.txt
echo "Quarterly summary: invented sample text for a lab" > summary.txt
echo "Draft notes with no predictable access pattern" > notes.txt
aws s3 cp app-1.txt s3://$BUCKET/logs/app-1.txt
aws s3 cp summary.txt s3://$BUCKET/reports/summary.txt --storage-class STANDARD_IA
aws s3 cp notes.txt s3://$BUCKET/notes/notes.txt --storage-class INTELLIGENT_TIERING
The log file uses the default class, S3 Standard, for data read often. The summary goes to Standard-IA, for data read rarely but needed at once. The notes go to Intelligent-Tiering, for data whose access pattern nobody knows. 4. List the objects with their storage classes:
aws s3api list-objects-v2 --bucket $BUCKET --query 'Contents[].[Key,StorageClass]' --output table
Each key shows its class: STANDARD, STANDARD_IA and INTELLIGENT_TIERING. The logs/, reports/ and notes/ parts of the keys are prefixes, not real folders, and a lifecycle rule can target one. 5. Write a lifecycle configuration. The first rule applies only to the logs/ prefix: after 30 days it moves objects to Standard-IA, after 180 days to S3 Glacier Deep Archive, and after 365 days it deletes them. The second rule applies to the whole bucket and removes multipart uploads left unfinished for 7 days:
cat > lifecycle.json <<'EOF'
{
"Rules": [
{
"ID": "clf-lab18-logs-rule",
"Filter": {"Prefix": "logs/"},
"Status": "Enabled",
"Transitions": [
{"Days": 30, "StorageClass": "STANDARD_IA"},
{"Days": 180, "StorageClass": "DEEP_ARCHIVE"}
],
"Expiration": {"Days": 365}
},
{
"ID": "clf-lab18-abort-uploads",
"Filter": {"Prefix": ""},
"Status": "Enabled",
"AbortIncompleteMultipartUpload": {"DaysAfterInitiation": 7}
}
]
}
EOF
aws s3api put-bucket-lifecycle-configuration --bucket $BUCKET --lifecycle-configuration file://lifecycle.json
- Read the rules back, and see them in the console:
aws s3api get-bucket-lifecycle-configuration --bucket $BUCKET --query 'Rules[].[ID,Status]' --output table
Then open the S3 console, choose your bucket, and open the Management tab. Lifecycle rules lists clf-lab18-logs-rule and clf-lab18-abort-uploads, and choosing the first draws its transitions as a timeline. Nothing moves today: the rules act on objects by age, and these objects are minutes old.
Verify
Run these read-only checks. If CloudShell has restarted, set BUCKET again as in step 1 first.
aws s3api get-public-access-block --bucket $BUCKET --query 'PublicAccessBlockConfiguration.BlockPublicPolicy' --output json
Expected output: true.
aws s3api list-objects-v2 --bucket $BUCKET --query "Contents[?StorageClass=='STANDARD_IA'] | length(@)"
Expected output: 1.
aws s3api list-objects-v2 --bucket $BUCKET --query "Contents[?StorageClass=='INTELLIGENT_TIERING'] | length(@)"
Expected output: 1.
aws s3api get-bucket-lifecycle-configuration --bucket $BUCKET --query 'length(Rules)'
Expected output: 2.
Cost and clean-up
S3 storage and requests may be charged outside the AWS Free Tier. Two details matter here. S3 Standard-IA has a minimum storage duration and a minimum billable object size, so deleting the summary object today is charged as if a larger object had been kept for that minimum period. For one tiny object the amount is very small, but it is not zero. The other classes in this lab have no such minimum. The lab never puts an object directly into a Glacier class, whose minimum periods are longer. See the Amazon S3 pricing page or the AWS Pricing Calculator for current figures.
Created by this lab: the bucket clf-lab18-storage-111122223333, with your own account ID in the name; the objects logs/app-1.txt, reports/summary.txt and notes/notes.txt in it; the lifecycle rules clf-lab18-logs-rule and clf-lab18-abort-uploads on it; the working files app-1.txt, summary.txt, notes.txt and lifecycle.json in your CloudShell home directory.
- Empty and delete the bucket
clf-lab18-storage-111122223333(with your own account ID).--forcedeletes the objectslogs/app-1.txt,reports/summary.txtandnotes/notes.txtfirst, and the lifecycle rulesclf-lab18-logs-ruleandclf-lab18-abort-uploadsgo with the bucket:
aws s3 rb s3://clf-lab18-storage-111122223333 --force
- Remove the working files
app-1.txt,summary.txt,notes.txtandlifecycle.jsonfrom CloudShell:
rm -f app-1.txt summary.txt notes.txt lifecycle.json
Check it is gone
aws s3api list-buckets --query "Buckets[?Name=='clf-lab18-storage-111122223333'] | length(@)"
Use your own account ID in the name. Expected output: 0.
In the S3 console, the General purpose buckets list no longer shows a bucket whose name starts with clf-lab18.
Notes
Objects smaller than 128 KB are never moved between Intelligent-Tiering's access tiers and are not charged its monitoring fee; they simply stay in the frequent-access tier. Lifecycle transitions likewise skip very small objects by default. Neither matters for this lab, which deletes everything within the hour, but it explains why tiny objects are a poor fit for the cheaper classes. Versioning is left off, so --force removes every object.
This is an independent study companion for the AWS Certified Cloud Practitioner (CLF-C02) exam and is not produced by or endorsed by Amazon Web Services. AWS, Amazon Web Services and AWS Certified Cloud Practitioner are trademarks of Amazon.com, Inc. or its affiliates.
This is an independent study companion for AWS Certified Cloud Practitioner CLF-C02 and is not produced by or endorsed by CompTIA.