Lab · Task 1.1 · about 35 minutes
Least-Privilege Role and the Explicit Deny
Services in this lab
- IAMin scope
- Amazon S3in scope
- AWS CLIin scope
Lesson for this lab: Secure Access to AWS Resources
Goal
Build a least-privilege IAM role, assume it with AWS STS, and watch both an implicit deny and an explicit deny in a bucket policy stop it.
Before you start
From the exam's point of view, this lab proves three lines from the lesson. A role gets temporary credentials, not keys. Anything the role's policy doesn't allow is denied by default. An explicit Deny in a resource-based policy beats an Allow in the identity policy.
- Use a sandbox AWS account, not production, and set an AWS Budgets alert on it.
- Sign in as an administrator through IAM Identity Center, not as the root user. For the CLI, use AWS CLI v2 with an IAM Identity Center profile (
aws configure sso), so you don't create access keys for this lab. If you don't have IAM Identity Center set up, enable it and create one user with anAdministratorAccesspermission set first. 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 account ID with
aws sts get-caller-identity --query Account --output text. Below, replace<account-id>with it and<your-bucket-name>with a globally unique bucket name. - Open two terminal windows. Terminal 1 is your administrator. Terminal 2 will hold only the role's temporary credentials, so it must not inherit
AWS_PROFILEfrom your shell: step 4 unsets it there before exporting the role's keys.
Resources this lab creates, all deleted in Clean up:
| Resource | Name |
|---|---|
| S3 bucket | <your-bucket-name> |
| S3 object | hello.txt |
| IAM role | saa-lab-reader |
| Inline role policy | read-one-bucket |
| Bucket policy | Added in step 6, removed in step 8 |
| Local files | trust.json, read.json, deny.json, hello.txt, copy.txt |
Steps
0 of 8 steps done
In terminal 1 (administrator):
echo "secure access lab" > hello.txt aws s3 mb s3://<your-bucket-name> --region <region> aws s3 cp hello.txt s3://<your-bucket-name>/hello.txtConsole: S3 → Create bucket, keep Block Public Access on, then Upload
hello.txt.The trust policy says who may assume the role: principals in your own account whose identity policy allows
sts:AssumeRole. Your Identity Center administrator session already allows it.Save as
trust.json:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::<account-id>:root" }, "Action": "sts:AssumeRole" } ] }aws iam create-role --role-name saa-lab-reader \ --assume-role-policy-document file://trust.jsonConsole: IAM → Roles → Create role → AWS account → This account, name it
saa-lab-reader, and attach no policies yet.The permissions policy says what the role may do: list this one bucket and read its objects. Nothing else.
Save as
read.json:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::<your-bucket-name>" }, { "Effect": "Allow", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::<your-bucket-name>/*" } ] }aws iam put-role-policy --role-name saa-lab-reader \ --policy-name read-one-bucket --policy-document file://read.jsonConsole: open the role, Add permissions → Create inline policy → JSON, paste the policy, name it
read-one-bucket.In terminal 1, request temporary credentials:
aws sts assume-role \ --role-arn arn:aws:iam::<account-id>:role/saa-lab-reader \ --role-session-name lab-session \ --query Credentials --output jsonThe output holds
AccessKeyId,SecretAccessKey,SessionTokenand anExpirationtime. In terminal 2, export them and confirm who you are. Unsetting the profile also drops its Region, so setAWS_REGIONagain:unset AWS_PROFILE export AWS_REGION=<region> export AWS_ACCESS_KEY_ID=<AccessKeyId> export AWS_SECRET_ACCESS_KEY=<SecretAccessKey> export AWS_SESSION_TOKEN=<SessionToken> aws sts get-caller-identity aws s3api get-object --bucket <your-bucket-name> --key hello.txt copy.txtget-caller-identityshows an ARN of the formarn:aws:sts::<account-id>:assumed-role/saa-lab-reader/lab-session, and theget-objectcall writescopy.txt. Your administrator session signed in through Identity Center is itself a role session, so this is role chaining and the new session lasts at most one hour.Console alternative: account menu → Switch role, enter your account ID and
saa-lab-reader, then open the bucket and downloadhello.txt.EXPECTED FAILURE. In terminal 2, try to list every bucket and to upload a file:
aws s3api list-buckets aws s3 cp hello.txt s3://<your-bucket-name>/upload.txtBoth fail with an
AccessDeniederror. Nothing inread-one-bucketallows those actions, so the role is not authorized for them and they start and stay denied. That is an implicit deny.In terminal 1 (administrator), save as
deny.json:{ "Version": "2012-10-17", "Statement": [ { "Sid": "DenyLabReaderGet", "Effect": "Deny", "Principal": { "AWS": "arn:aws:iam::<account-id>:role/saa-lab-reader" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::<your-bucket-name>/*" } ] }aws s3api put-bucket-policy --bucket <your-bucket-name> --policy file://deny.jsonConsole: bucket → Permissions → Bucket policy → Edit, paste the policy, save.
EXPECTED FAILURE. In terminal 2, repeat the read that worked in step 4:
aws s3api get-object --bucket <your-bucket-name> --key hello.txt copy.txtIt now fails with an
AccessDeniederror, even though the role's identity policy has not changed. The role's identity policy still allowss3:GetObject, but an explicit deny in any policy ends the evaluation. This is the lesson's trap "adding anAllowto override aDeny" seen from the other side. Policy changes are eventually consistent, so if the read still succeeds, wait a few seconds and retry.In terminal 1:
aws s3api delete-bucket-policy --bucket <your-bucket-name>In terminal 2, run the same
get-objectcommand. It succeeds again, because only the identity policy's allow remains.
Verify
Run each check and compare it with the expected result.
| Check | Terminal | Expected result |
|---|---|---|
aws sts get-caller-identity | 2 | ARN contains assumed-role/saa-lab-reader/lab-session |
aws s3api get-object --bucket <your-bucket-name> --key hello.txt copy.txt | 2 | Succeeds and writes copy.txt containing secure access lab |
aws s3api list-buckets | 2 | AccessDenied (implicit deny) |
aws iam list-role-policies --role-name saa-lab-reader | 1 | "PolicyNames": ["read-one-bucket"] |
aws iam list-attached-role-policies --role-name saa-lab-reader | 1 | "AttachedPolicies": [], so the inline policy is the role's only grant |
aws s3api get-bucket-policy --bucket <your-bucket-name> | 1 | NoSuchBucketPolicy error, since the deny was removed in step 8 |
Clean up
Delete everything in this order. The bucket must be empty before you delete it, and the role's inline policy must go before the role.
-
Terminal 2: drop the temporary credentials, then close the window.
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN -
Terminal 1: remove the bucket policy if it's still there (an error saying no policy exists is fine).
aws s3api delete-bucket-policy --bucket <your-bucket-name> -
Delete the object and the bucket.
--forceempties the bucket first.aws s3 rb s3://<your-bucket-name> --force -
Delete the inline policy, then the role.
aws iam delete-role-policy --role-name saa-lab-reader --policy-name read-one-bucket aws iam delete-role --role-name saa-lab-reader -
Delete the local files:
trust.json,read.json,deny.json,hello.txtandcopy.txt. -
Check that nothing remains:
aws iam get-role --role-name saa-lab-reader aws s3api head-bucket --bucket <your-bucket-name>The first command returns
NoSuchEntityand the second returns a404(Not Found) error. Console: IAM → Roles no longer listssaa-lab-reader, and S3 → Buckets no longer lists your bucket. The lab created no other resources, so nothing from this lab should remain.
Sources
- https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic.html
- https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_access-denied.html
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/troubleshoot-403-errors.html
- https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role.html
- https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html
- https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-console.html
- https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot.html
- https://docs.aws.amazon.com/singlesignon/latest/userguide/enable-identity-center.html
Last reviewed