Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

S3 Versioning, Lifecycle Rules and Replication (SRR and CRR) Explained

In How to Host a Static Website on S3 we served files to the world. Now we protect them from mistakes and control cost over time: versioning, delete markers, MFA Delete, lifecycle rules, and replication to another bucket in the same Region (SRR) or another Region (CRR).

In this chapter

  • Why versioning saves you from deletes and overwrites
  • Delete markers and restoring old versions
  • MFA Delete: what and who can enable it
  • Lifecycle rules: transitions and expirations
  • SRR vs CRR replication

1. A school-project story

A student saves project.docx, then saves a broken copy over it the night before submission. On a normal disk, the good copy is gone. With "version history" in Google Docs, she opens yesterday's version and is saved.

S3 versioning is that version history for every object in a bucket.

2. Versioning

Apps overwrite files by bug, people run aws s3 rm --recursive on the wrong prefix, ransomware overwrites data. Versioning keeps every old copy.

StateMeaning
Unversioned (default)One copy per key; overwrite and delete are final
EnabledEvery PUT makes a new version ID; old ones stay
SuspendedNew writes get version ID "null"; old versions stay. You can never go back to Unversioned.

Each version is billed as storage. A 100 MB file overwritten 10 times = 1 GB. That is why versioning and lifecycle go together.

See the versions behind one object key

In a bucket with versioning enabled, replacing a file and deleting its name have different effects. Follow results.csv without permanently deleting any version.

See the versions behind one object keyUpload version A: results.csv contains the first marks; Upload version B: Same key; new current version; Delete without version ID: A delete marker becomes current; Remove that delete marker: Previous data version becomes currentUpload version Aresults.csv contains the first marksUpload version BSame key; new current versionDelete without version IDA delete marker becomes currentRemove that delete markerPrevious data version becomes current
Read all steps
  1. Save the first version

    Uploading results.csv creates a data version. Record the version ID in the console or API result. That ID identifies this particular stored version, not the whole history of the key.

  2. Replace the contents

    Uploading corrected marks under the same key creates another version. A normal GET now returns the current version B, while version A remains available by its version ID and continues using storage.

  3. Delete the key normally

    A delete request without a version ID adds a delete marker. A normal GET now behaves as though the object is deleted. The older data versions have not thereby been erased.

  4. Restore visibility carefully

    Remove the specific current delete marker to expose the preceding data version again. Deleting a particular data version instead permanently removes that version. Cleanup must account for versions and delete markers, not only the default object list.

3. Delete markers

Draw one key above a stack of versions

Take homework/report.pdf. Write version A at the bottom when the first file is uploaded. A new upload adds version B on top. A normal GET reads the top data version. A normal delete in a versioning-enabled bucket adds a delete marker above B. The default GET can now report that the object is missing even though A and B are still stored.

In a fresh practice bucket, use the console’s version view to identify the exact marker. Have the authorized tester remove that marker and request the key again. B becomes visible. Removing A or B by version ID is a different operation that can permanently delete that specific version. Record the key and version IDs before the drill so you can tell which one you are about to act on.

With versioning on, a normal delete must be undoable.

A DELETE without a version ID does not erase data. S3 adds a delete marker as the newest version, so GET returns 404. Delete the marker and the object is back. A DELETE with a version ID permanently removes that version.

Life of report.csvPUT v1first uploadPUT v2overwrite, v1 keptDELETEdelete marker on topGET → 404Delete the markerv2 is current again
Nothing was lost until a version ID was deleted.

4. MFA Delete

If an attacker steals admin keys, they can delete versions permanently. MFA Delete demands a code from a physical or virtual MFA device for that.

  • Needs an MFA code to permanently delete a version or to change the versioning state.
  • Only the root user can enable it, and only through the CLI/API, not the console.
  • aws s3api put-bucket-versioning --bucket b --versioning-configuration Status=Enabled,MFADelete=Enabled --mfa "arn:aws:iam::111122223333:mfa/root-account-mfa-device 123456"
  • Lifecycle rules cannot be used on a bucket with MFA Delete. For many teams, Object Lock (WORM retention) is the modern alternative.

5. Lifecycle rules

Write retention rules for old data before relying on them

Think about the two copies of report.pdf after an update. B is current and A is noncurrent. A rule aimed at current objects and a rule aimed at old versions answer different questions. Keeping old versions forever uses storage. Removing them too quickly can shorten the time available to recover from an accidental overwrite.

Use a unique lab prefix for a test rule and state the expected result before saving it. For example, old versions under that prefix can expire after a chosen recovery window while current versions remain. A lifecycle action is managed by S3 and may not appear immediately at the boundary time. Confirm the scope and schedule separately from an immediate manual delete test.

Logs are read in the first week, rarely after a month, and never after a year. Paying Standard prices forever wastes money.

A rule targets a prefix or tag and does transitions (move to a cheaper class after N days) and expirations (delete after N days). It can also expire noncurrent versions and clean up incomplete multipart uploads.

DataRuleWhy
logs/30 d → Standard-IA, 90 d → Glacier Flexible, 365 d → expireRead less over time
backups/mysql/1 d → Glacier Instant, 180 d → Deep ArchiveKept for audit, rarely restored
Noncurrent versionsExpire 30 days after becoming noncurrent, keep 3 newestVersioning cost under control
Incomplete multipartAbort after 7 daysHidden storage you pay for

Ravindra Bagale's Tip

Remember minimum days: IA classes 30, Glacier Instant/Flexible 90, Deep Archive 180. S3 will not transition to Standard-IA before 30 days in Standard. Small objects under 128 KB are not transitioned by default.

{"Rules":[{"ID":"logs-tiering","Status":"Enabled","Filter":{"Prefix":"logs/"},
 "Transitions":[{"Days":30,"StorageClass":"STANDARD_IA"},{"Days":90,"StorageClass":"GLACIER"}],
 "Expiration":{"Days":365},
 "NoncurrentVersionExpiration":{"NoncurrentDays":30,"NewerNoncurrentVersions":3},
 "AbortIncompleteMultipartUpload":{"DaysAfterInitiation":7}}]}

6. Replication: SRR and CRR

A copy in another Region survives a Regional outage and meets compliance. A copy in the same Region can feed a separate prod/test or log account.

PointSRRCRR
DestinationSame RegionAnother Region
UseLog aggregation, prod → test copy, data sovereigntyDisaster recovery, lower latency elsewhere, compliance
Extra costDestination storage + requests+ inter-Region transfer per GB
  • Versioning must be on in both buckets.
  • S3 needs an IAM role to read source and write destination (the console can create it).
  • Only new objects replicate. Use S3 Batch Replication for existing ones.
  • Deletes with a version ID are not replicated (protects the copy). Delete markers replicate only if you turn that on.
  • Replication Time Control (RTC) gives a 15-minute SLA for 99.99% of objects, at extra cost.
CRR for DRApp uploadsto Mumbai bucketSource bucketap-south-1, versionedReplication rulerole + filterDestination bucketap-southeast-1, versionedRegion outage?read from Singapore
Replication is asynchronous, usually seconds to minutes.

7. Lab: versioning, restore, lifecycle and CRR

  1. Create yourname-ver-src in ap-south-1 with Bucket Versioning: Enable.
  2. echo v1 > r.txt; aws s3 cp r.txt s3://yourname-ver-src/; echo v2 > r.txt; aws s3 cp r.txt s3://yourname-ver-src/.
  3. aws s3api list-object-versions --bucket yourname-ver-src --query "Versions[].[VersionId,IsLatest]". Expected: two IDs, one true.
  4. Read the old one: aws s3api get-object --bucket yourname-ver-src --key r.txt --version-id <old-id> old.txt; cat old.txt. Expected: v1.
  5. Delete: aws s3 rm s3://yourname-ver-src/r.txt, then aws s3 ls s3://yourname-ver-src/. Expected: empty.
  6. Find the marker: aws s3api list-object-versions --bucket yourname-ver-src --query "DeleteMarkers[].VersionId". Remove it: aws s3api delete-object --bucket yourname-ver-src --key r.txt --version-id <marker-id>. aws s3 ls shows r.txt again.
  7. Management › Create lifecycle rule: name logs-tiering, prefix logs/, transition current versions to Standard-IA at 30 days, Glacier Flexible Retrieval at 90, expire at 365, permanently delete noncurrent after 30 days, delete incomplete multipart after 7 days. Save.
  8. Verify: aws s3api get-bucket-lifecycle-configuration --bucket yourname-ver-src.
  9. Create yourname-ver-dst in ap-southeast-1, versioning Enable.
  10. Source › Management › Replication rules › Create: name to-singapore, scope all objects, destination yourname-ver-dst, IAM role = Create new role. Answer No to replicating existing objects. Save.
  11. echo dr > dr.txt; aws s3 cp dr.txt s3://yourname-ver-src/. After a minute: aws s3api head-object --bucket yourname-ver-src --key dr.txt --query ReplicationStatus. Expected: "COMPLETED". aws s3 ls s3://yourname-ver-dst/ --region ap-southeast-1 shows dr.txt; r.txt is not there (old object).

Cleanup

  1. Delete the replication rule. Empty both buckets with the console Empty button (it deletes all versions), then delete them.
  2. IAM › Roles › delete the s3crr_role… created for replication.

Why Empty and not aws s3 rb --force? On a versioned bucket, rb --force leaves old versions and markers, so the delete fails.

8. Interview questions

Q. Can you disable versioning?

No, only suspend it. Old versions stay.

Q. What is a delete marker?

A placeholder version added by a delete without a version ID. GET returns 404; deleting the marker restores the object.

Q. Who can enable MFA Delete and how?

The root user, via CLI/API with the --mfa parameter.

Q. Replication is set up but old files are missing in the destination. Why?

Replication only copies new objects. Use S3 Batch Replication.

Q. How do you stop versioning costs from growing?

Lifecycle rule: expire noncurrent versions after N days, keep a few newest.

Try at home

  1. Add an SRR rule to a second bucket in the same Region, filtered by tag env=prod.
  2. Write a lifecycle rule in JSON and apply it with put-bucket-lifecycle-configuration.

In short

  1. Versioning keeps every version; it can be suspended, never removed.
  2. A plain delete adds a delete marker; delete the marker to restore.
  3. MFA Delete: root user, CLI only.
  4. Lifecycle moves data to cheaper classes and deletes it on schedule.
  5. SRR same Region, CRR other Region; both need versioning; only new objects.

Practise this chapter

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Versions A and B exist for one key in a versioning-enabled bucket. A normal delete adds marker M. What does a GET without a version ID usually show?

Answer and explanation

C. The object appears missing

The current delete marker hides the object from normal access. The prior versions can still be retained.

2. How do you reveal B again if M is the current marker and B is still present?

Answer and explanation

A. Remove the exact marker M after checking its version ID

Removing the marker restores the preceding version to normal visibility. Removing a data version is a separate irreversible action.

3. A lifecycle rule is intended to clear older versions but keep the latest copy. What should it target?

Answer and explanation

D. Noncurrent versions for the intended keys

Old data versions and the current one are evaluated separately. A noncurrent-version rule can preserve the current copy.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Sydney Surf School is a sports training business in Sydney. Liam O'Brien looks after its IT and cloud work. They count money in AUD and run their AWS resources in the ap-southeast-2 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy CloudFormation: a versioned bucket

Infrastructure as code makes setups repeatable.

Write a CloudFormation YAML template with one S3 bucket named sydneysurf-docs that has versioning Enabled.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium Turn on versioning

Versioning keeps old copies, so an overwrite or delete can be undone.

Enable versioning on bucket sydneysurf-invoices-2026.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium List object versions

To restore an old copy you first find its version ID.

List the versions of objects under prefix price-list in bucket sydneysurf-invoices-2026.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Read an object's metadata

head-object shows size, type and version without downloading the file.

Show the metadata of key reels/v1.mp4 in bucket sydneysurf-media.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Attach a bucket policy from a file

Long JSON is easier to keep in a file; file:// tells the CLI to read it.

Apply the bucket policy saved in policy.json to bucket sydneysurf-web.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Storage cost

Storage is billed per GB-month.

500 GB in S3 Standard at USD 0.023 per GB-month. Monthly cost in USD?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Bucket policy for a public website

A static website bucket must let everyone read its files.

Write a bucket policy that lets anyone (*) do s3:GetObject on every object in sydneysurf-site.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Upload-only to one folder

Field staff can drop files but cannot read or delete anything.

Allow s3:PutObject only under the prefix uploads/ in bucket sydneysurf-data.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard S3 lifecycle rule (JSON)

Old files move to cheaper storage automatically.

Write lifecycle JSON: one Enabled rule, prefix logs/, transition to GLACIER after 90 days.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Hard Apply lifecycle rules from a file

Putting a lifecycle configuration replaces the full rule set. Review any existing rules before changing a real bucket.

In the answer box, write the AWS CLI command that applies the complete configuration in lifecycle.json to the disposable lab bucket sydneysurf-logs. Assume the lab bucket has no existing lifecycle rules.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Turn on replication

Replication copies new objects to another bucket, even in another Region (CRR).

Apply the replication configuration in replication.json to bucket sydneysurf-invoices-2026.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Deny requests without HTTPS

aws:SecureTransport false means plain HTTP; deny it to protect data in transit.

Bucket policy: deny s3:* for everyone (*) on sydneysurf-data and its objects when aws:SecureTransport is false.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: S3 versioning. Every overwrite keeps the old version. A plain delete adds a delete marker; GET gives 404. Delete the marker and the object is back. Can be suspended, never turned off.
Revision card: S3 versioning · Download PNG
Revision card: S3 lifecycle rules. Transitions move data to cheaper classes after N days. Expirations delete data after N days. Also expire noncurrent versions and abort old multipart uploads. logs/: 30 d IA, 90 d Glacier, 365 d delete.
Revision card: S3 lifecycle rules · Download PNG
Revision card: S3 replication SRR and CRR. SRR copies to a bucket in the same Region; CRR to another Region. Versioning must be on in both buckets. Only new objects copy. Use Batch Replication for old ones. CRR = disaster recovery in another Region.
Revision card: S3 replication SRR and CRR · Download PNG
Comparison: SRR vs CRR. SRR: Replicates new objects to a bucket in the same Region. CRR: Replicates new objects to a bucket in another Region.
Comparison: SRR vs CRR · Download PNG
Comparison: Versioning enabled vs Versioning suspended. Versioning enabled: Each PUT creates a new version ID; old ones stay. Versioning suspended: New writes get version ID null; existing versions stay.
Comparison: Versioning enabled vs Versioning suspended · Download PNG
Interview card: Someone deleted files from a versioned bucket. Recover?. A plain delete only added delete markers. List DeleteMarkers and delete those marker versions. The previous versions become current again.
Interview card: Someone deleted files from a versioned bucket. Recover? · Download PNG
Interview card: CRR is on, but old files are missing in the target. Why?. Replication only copies objects created after the rule. Run S3 Batch Replication for existing objects. Check versioning is on in both buckets and the role can write.
Interview card: CRR is on, but old files are missing in the target. Why? · Download PNG

More cards: Ravi Sir's Cards.