203 — S3 Versioning and Lifecycle Rules

Intermediate

Enable S3 versioning, upload multiple object versions, restore a previous version by ID, and configure a lifecycle rule to expire non-current versions — all from the CLI.

Learning Objectives

1
Enable S3 versioning on a bucket using the AWS CLI
2
Upload multiple versions of the same object and observe version IDs
3
List all object versions with aws s3api list-object-versions
4
Restore a specific previous version of an object by version ID
5
Write and apply a lifecycle rule to expire non-current versions after 30 days
6
Verify the lifecycle configuration and clean up all object versions
Step 1

Create the versioning lab bucket

Create a new S3 bucket for this lesson. The name uses your Account ID to guarantee global uniqueness.

Commands to Run

BUCKET="devopspath-$(aws sts get-caller-identity \
  --query Account \
  --output text)-versioning-lab"
echo "Bucket name: $BUCKET"
aws s3 mb s3://$BUCKET --region us-east-1

What This Does

The BUCKET shell variable stores the unique bucket name for the rest of this lesson.

Using your 12-digit Account ID in the name guarantees global uniqueness without a random suffix.

The variable lives in your current shell session — if you open a new terminal window you will need to re-run the first command to restore it.

Expected Outcome

The echo command prints a name like: devopspath-123456789012-versioning-lab

The mb command outputs: make_bucket: devopspath-123456789012-versioning-lab

Pro Tips

  • 1
    S3 billing callout: versioned buckets accumulate storage for every version of every object, which drains Free Tier credits (current Free Plan) or eats the legacy 5 GB/12-month allowance faster than you expect. Run the cleanup step at the end of this lesson.
  • 2
    Re-export BUCKET if you close and reopen your terminal: BUCKET="devopspath-$(aws sts get-caller-identity --query Account --output text)-versioning-lab"

Common Mistakes to Avoid

  • āš ļøClosing the terminal between steps and losing the BUCKET variable — all subsequent commands will fail with 'NoSuchBucket' if $BUCKET is empty. Run the first command again to restore it.
Was this step helpful?

All Steps (0 / 11 completed)