Lab · Task 1.3 · about 30 minutes
Object Lock, Legal Hold and Block Public Access
Services in this lab
- AWS KMSin scope
- Amazon S3in scope
- AWS CLIin scope
Lesson for this lab: Data Security Controls
Goal
Build one bucket that encrypts with a customer managed key, locks its objects, and refuses public access, then watch a locked version and a public policy both get refused.
Before you start
From the exam's point of view, this lab proves three lines from the lesson. A key policy must allow the access that you want. Object Lock in governance mode can be bypassed only by a caller who asks for it. Block Public Access refuses a public bucket policy even when the policy is valid JSON. The lab also runs one lab-only check that the lesson does not teach: a legal hold stays in place even when a bypass is requested.
- Use a sandbox AWS account, not production, and set an AWS Budgets alert on it.
- Sign in as an administrator through IAM Identity Center, as in the 1.1 lab. Do not create IAM user access keys.
- Pick one Region and use it throughout, for example
eu-west-1. Below,<region>means that Region. - Find your 12-digit account ID with
aws sts get-caller-identity --query Account --output text, and write it down as<account-id>. Do not paste it into any public place. - This lab uses governance mode on purpose. Compliance mode cannot be removed before its retention date, so a lab bucket in compliance mode could not be emptied or deleted during clean-up.
- Write the IDs and names that each command returns into a notes file, because later steps refer to them as
<key-id>,<key-arn>,<version-id>and<version-id-2>.
Resources this lab creates, all removed in Clean up:
| Resource | Name or placeholder |
|---|---|
| KMS key | <key-id>, alias alias/lab-data-security |
| S3 bucket | <your-bucket-name>, with Object Lock enabled |
| S3 object | records/patient-001.csv, with one version |
| S3 object | records/patient-002.csv, deleted during the steps |
| Local files | key-policy.json, lock-config.json, record.csv |
| Local files | record-2.csv, public-policy.json |
Steps
0 of 8 steps done
Save this as
key-policy.jsonand replace<account-id>with your own account ID. The statement names the account root, which is what lets IAM policies in the account grant use of the key later. Without it, an IAM policy that allowskms:Decryptwould grant nothing.{ "Version": "2012-10-17", "Statement": [ { "Sid": "EnableIamPoliciesInThisAccount", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::<account-id>:root" }, "Action": "kms:*", "Resource": "*" } ] }In terminal 1, create the key, give it an alias, and turn on automatic rotation:
aws kms create-key --policy file://key-policy.json \ --description "Lab key for data security" \ --query KeyMetadata.KeyId --output text aws kms create-alias --alias-name alias/lab-data-security --target-key-id <key-id> aws kms enable-key-rotation --key-id <key-id> aws kms describe-key --key-id <key-id> --query KeyMetadata.Arn --output textWrite the last command's output down as
<key-arn>. Console: KMS → Create key → Symmetric → Key administrators and key users is the usual path, but the CLI shows the key policy as a file you can read.Object Lock is set when the bucket is created, and it works only on a versioned bucket. The next command turns versioning on explicitly. In terminal 1:
aws s3api create-bucket --bucket <your-bucket-name> --region <region> \ --create-bucket-configuration LocationConstraint=<region> \ --object-lock-enabled-for-bucket aws s3api put-bucket-versioning --bucket <your-bucket-name> \ --versioning-configuration Status=Enabled aws s3api put-public-access-block --bucket <your-bucket-name> \ --public-access-block-configuration \ BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true aws s3api put-bucket-encryption --bucket <your-bucket-name> \ --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"aws:kms","KMSMasterKeyID":"<key-arn>"},"BucketKeyEnabled":true}]}'Console: S3 → Create bucket, expand Advanced settings, turn on Object Lock, and keep Block all public access on. Then open Properties and confirm the default encryption is SSE-KMS with your key.
Save this as
lock-config.json. Governance mode is short here so that clean-up stays possible. Production compliance rules would useCOMPLIANCEand a longer period.{ "ObjectLockEnabled": "Enabled", "Rule": { "DefaultRetention": { "Mode": "GOVERNANCE", "Days": 1 } } }In terminal 1, apply it, write a small record, and read back what the bucket did to it:
aws s3api put-object-lock-configuration --bucket <your-bucket-name> \ --object-lock-configuration file://lock-config.json echo "patient-001,example" > record.csv aws s3api put-object --bucket <your-bucket-name> --key records/patient-001.csv \ --body record.csv --query VersionId --output text aws s3api head-object --bucket <your-bucket-name> --key records/patient-001.csv \ --query '{Encryption:ServerSideEncryption,Key:SSEKMSKeyId,Mode:ObjectLockMode,Until:ObjectLockRetainUntilDate}'Write the
VersionIdas<version-id>. Thehead-objectoutput shows the object is encrypted with SSE-KMS under your key, and that a retention mode and an until-date were applied from the bucket default. You did not set them on the object yourself.A legal hold is a separate switch from retention. It has no end date. In terminal 1:
aws s3api put-object-legal-hold --bucket <your-bucket-name> \ --key records/patient-001.csv --version-id <version-id> \ --legal-hold Status=ON aws s3api get-object-legal-hold --bucket <your-bucket-name> \ --key records/patient-001.csv --version-id <version-id>The second command prints
"Status": "ON". The hold stays until someone sets it toOFF, whatever the retention date says.Governance mode lets a caller remove a locked version if it asks with the bypass flag and holds the
s3:BypassGovernanceRetentionpermission. An administrator role withAdministratorAccesshas that permission. This version also carries a legal hold, which the bypass does not cover. In terminal 1, try it:aws s3api delete-object --bucket <your-bucket-name> \ --key records/patient-001.csv --version-id <version-id> \ --bypass-governance-retentionEXPECTED FAILURE. The call fails with an
AccessDenied(403) error. The legal hold stays in place, because bypassing governance mode doesn't affect a legal hold. This check is specific to the lab and is not taught in the lesson: a bypass removes a retention period, and it does not clear a legal hold.Run the same delete without the flag and it is refused too. A legal hold blocks a permanent delete even when no retention period is set.
This step shows what the bypass flag is for. Write a second record. It picks up the same one-day governance retention from the bucket default, but it has no legal hold. In terminal 1:
echo "patient-002,example" > record-2.csv aws s3api put-object --bucket <your-bucket-name> --key records/patient-002.csv \ --body record-2.csv --query VersionId --output textWrite the
VersionIdas<version-id-2>. Now delete that version without the bypass flag:aws s3api delete-object --bucket <your-bucket-name> \ --key records/patient-002.csv --version-id <version-id-2>EXPECTED FAILURE. The call fails with an
AccessDenied(403) error. Governance retention protects the version from any caller who does not ask to bypass it, administrators included.Run the same delete with the bypass flag:
aws s3api delete-object --bucket <your-bucket-name> \ --key records/patient-002.csv --version-id <version-id-2> \ --bypass-governance-retentionThis time it succeeds and prints the deleted
VersionId. Your role holdss3:BypassGovernanceRetentionand asked for the bypass, and no legal hold stands in the way. Compliance mode would have refused both calls.Save this as
public-policy.json. It is valid JSON that would make every object public:{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicRead", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::<your-bucket-name>/*" } ] }In terminal 1, try to attach it:
aws s3api put-bucket-policy --bucket <your-bucket-name> --policy file://public-policy.jsonEXPECTED FAILURE. The call fails with an
AccessDeniederror, because the bucket's Block Public Access setting refuses any bucket policy that allows public access. In terminal 1, confirm the policy never landed:aws s3api get-bucket-policy --bucket <your-bucket-name>It returns
NoSuchBucketPolicy, which proves the block stopped the change before it took effect.Clean-up needs the hold lifted first. In terminal 1:
aws s3api put-object-legal-hold --bucket <your-bucket-name> \ --key records/patient-001.csv --version-id <version-id> \ --legal-hold Status=OFFLeave the retention in place. Clean up removes the version with the bypass flag, which works once the hold is off, because the retention is governance mode.
Verify
Run each check and compare it with the expected result.
| Check | Terminal | Expected result |
|---|---|---|
aws kms get-key-rotation-status --key-id <key-id> --query KeyRotationEnabled --output text | 1 | Prints True |
aws s3api get-bucket-encryption --bucket <your-bucket-name> --query 'ServerSideEncryptionConfiguration.Rules[0].ApplyServerSideEncryptionByDefault.SSEAlgorithm' --output text | 1 | Prints aws:kms |
aws s3api get-public-access-block --bucket <your-bucket-name> --query 'PublicAccessBlockConfiguration.BlockPublicPolicy' --output text | 1 | Prints True |
aws s3api get-object-lock-configuration --bucket <your-bucket-name> --query 'ObjectLockConfiguration.ObjectLockEnabled' --output text | 1 | Prints Enabled |
aws s3api get-object-legal-hold --bucket <your-bucket-name> --key records/patient-001.csv --version-id <version-id> | 1 | After step 8, prints "Status": "OFF" |
aws s3api list-object-versions --bucket <your-bucket-name> --query 'Versions[].VersionId' --output text | 1 | Prints <version-id>, the locked version is still there |
Clean up
Delete everything in this order. Object Lock keeps the version until its one-day retention ends, so clean-up uses the bypass flag. A bucket with locked versions cannot be deleted until they are gone.
-
Terminal 1: remove the version. The legal hold is already off, so the bypass flag works. If you run this within the first day, the bypass is what lets you delete it.
aws s3api delete-object --bucket <your-bucket-name> \ --key records/patient-001.csv --version-id <version-id> \ --bypass-governance-retention -
Terminal 1: confirm no versions or delete markers remain, then delete the bucket.
aws s3api list-object-versions --bucket <your-bucket-name> \ --query '{Versions:Versions[].VersionId,Markers:DeleteMarkers[].VersionId}' aws s3api delete-bucket --bucket <your-bucket-name>Both lists should print
nullor[]before the delete. -
Terminal 1: schedule the key for deletion, and remove the alias so the name is free for later labs. KMS holds a key in a waiting period of 7 to 30 days before it is removed, and this lab uses the shortest period.
aws kms delete-alias --alias-name alias/lab-data-security aws kms schedule-key-deletion --key-id <key-id> --pending-window-in-days 7 -
Terminal 1: delete the local files you created.
rm -f key-policy.json lock-config.json public-policy.json record.csv record-2.csv -
Final check: confirm the bucket is gone and the key is pending deletion, then stop.
aws s3api head-bucket --bucket <your-bucket-name> aws kms describe-key --key-id <key-id> --query KeyMetadata.KeyState --output textThe first command returns a
404orNot Founderror, which means the bucket no longer exists. The second printsPendingDeletion. Nothing else from this lab should remain in the account.
Sources
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lock.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lock-configure.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lock-managing.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-control-block-public-access.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/configuring-block-public-access-account.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucket-encryption.html
- https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html
- https://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html
- https://docs.aws.amazon.com/kms/latest/developerguide/deleting-keys-scheduling-key-deletion.html
Last reviewed