Meet a recovery requirement by proving you can retrieve the expected bytes. When someone overwrites a small object in a versioned Amazon S3 bucket, its older version can remain available. A normal delete can then hide the key behind a delete marker, even though the underlying versions still exist.
This sandbox drill writes a known-good file, overwrites it, adds a delete marker, and restores the original bytes by version ID. It gives you a concrete check to record in a recovery runbook.
Prerequisites
- AWS CLI v2, a Bash shell, and a sandbox account in which you can create and remove a private, general-purpose S3 bucket. The lab uses
us-east-1. - Permission to manage versioning on the lab bucket, list object versions, read and write objects, and remove the lab’s versions and delete marker. Listing versions needs
s3:ListBucketVersions. Reading a specific version needss3:GetObjectVersion. - A local
cmpcommand for checking the recovered bytes. Use synthetic data for the exercise.
Keep every step in the same shell and continue after each command succeeds. The commands are scoped to a single lab bucket and one object key.
Set up a disposable versioned bucket
Confirm your AWS identity and choose a globally unique bucket name. The example creates a general-purpose bucket in US East (N. Virginia), where the create command omits LocationConstraint.
REGION=us-east-1
aws sts get-caller-identity
ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
BUCKET="tm-restore-lab-${ACCOUNT}-$(date -u +%Y%m%d%H%M%S)-${RANDOM}"
KEY=drill/config.txt
aws s3api create-bucket --bucket "$BUCKET" --region "$REGION"
aws s3api put-bucket-versioning --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT" --versioning-configuration Status=Enabled
aws s3api get-bucket-versioning --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT"
Confirm the output reports Enabled. AWS recommends waiting 15 minutes after enabling versioning for the first time before issuing object writes or deletes. The setting can appear enabled before the change has propagated fully. AWS guidance for enabling versioning.
Simulate an overwrite and a soft delete
The following commands save the version IDs of two small writes and the delete marker. Confirm that each ID is non-empty and differs from the text None before proceeding.
LAB_DIR=$(mktemp -d)
printf 'tier=gold\n' > "$LAB_DIR/known-good.txt"
printf 'tier=invalid\n' > "$LAB_DIR/overwrite.txt"
GOOD_VERSION=$(aws s3api put-object --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT" --key "$KEY" --body "$LAB_DIR/known-good.txt" \
--query VersionId --output text)
BAD_VERSION=$(aws s3api put-object --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT" --key "$KEY" --body "$LAB_DIR/overwrite.txt" \
--query VersionId --output text)
MARKER=$(aws s3api delete-object --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT" --key "$KEY" --query VersionId --output text)
printf 'Good: %s\nOverwrite: %s\nMarker: %s\n' \
"$GOOD_VERSION" "$BAD_VERSION" "$MARKER"
List versions to see the difference between object versions and delete markers. The marker should be current, and both data versions should still be present. On a real bucket, compare exact keys as well as the prefix and use the application’s release timestamp or recorded version ID to identify the recovery candidate. AWS CLI reference for listing object versions.
aws s3api list-object-versions --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT" --prefix "$KEY" \
--query '{Versions:Versions[].{Key:Key,Id:VersionId,Current:IsLatest},Markers:DeleteMarkers[].{Key:Key,Id:VersionId,Current:IsLatest}}' \
--output json
Restore the known-good bytes
Retrieve the good version to a staging file by specifying its version ID, then compare it with the original. An ordinary read of this same key sees the current delete marker. AWS CLI reference for reading a specific version.
aws s3api get-object --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT" --key "$KEY" --version-id "$GOOD_VERSION" \
"$LAB_DIR/restored.txt"
cmp "$LAB_DIR/known-good.txt" "$LAB_DIR/restored.txt" && \
printf 'PASS: recovered bytes match the known-good file\n'
In an incident, establish the expected checksum or validate the restored file with the application before promoting it. Removing the marker in this scenario would expose the most recent data version, which contains the bad overwrite. To restore the live key after validation, a controlled upload of the verified data creates a new current version. With a real object, preserve relevant metadata and coordinate the write with the application owner. How S3 delete markers work.
Troubleshoot the drill
- A current read returns 404. Look for a current delete marker in the versions results. With restricted bucket-list permission a missing key can be reported as 403.
- A version-specific read returns AccessDenied. Check
s3:GetObjectVersionand, for an SSE-KMS object, the caller’s ability to decrypt with that key. - An expected version is absent. Check the exact key, preexisting unversioned objects, and whether lifecycle expiration or an explicit version deletion removed the expected data.
S3 Versioning helps with accidental overwrites and deletes inside one bucket. Plan separately for failures that affect the entire bucket or the credentials that control it. Record the time to find the correct version and the time to validate the recovered object. S3 Versioning behavior and limitations.
Clean up the lab
A versioned bucket keeps data versions and markers until you remove them or a lifecycle rule acts. Review the list and remove only the three IDs created by the drill. Confirm there are no other versions or markers before deleting the bucket. This does permanently remove the synthetic lab copies.
for id in "$GOOD_VERSION" "$BAD_VERSION" "$MARKER"; do
aws s3api delete-object --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT" --key "$KEY" --version-id "$id" || break
done
aws s3api list-object-versions --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT"
aws s3api delete-bucket --bucket "$BUCKET" --region "$REGION" \
--expected-bucket-owner "$ACCOUNT"
Normal S3 request and storage charges apply. In a long-lived versioned bucket, older data versions use storage too, so set a lifecycle policy that matches the recovery window you actually need. Amazon S3 pricing.







Leave a Reply