Skip to content
Certification Guide

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 an AdministratorAccess permission 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_PROFILE from your shell: step 4 unsets it there before exporting the role's keys.

Resources this lab creates, all deleted in Clean up:

ResourceName
S3 bucket<your-bucket-name>
S3 objecthello.txt
IAM rolesaa-lab-reader
Inline role policyread-one-bucket
Bucket policyAdded in step 6, removed in step 8
Local filestrust.json, read.json, deny.json, hello.txt, copy.txt

Steps

0 of 8 steps done

  1. 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.txt
    

    Console: S3 → Create bucket, keep Block Public Access on, then Upload hello.txt.

  2. 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.json
    

    Console: IAM → Roles → Create role → AWS account → This account, name it saa-lab-reader, and attach no policies yet.

  3. 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.json
    

    Console: open the role, Add permissions → Create inline policy → JSON, paste the policy, name it read-one-bucket.

  4. 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 json
    

    The output holds AccessKeyId, SecretAccessKey, SessionToken and an Expiration time. In terminal 2, export them and confirm who you are. Unsetting the profile also drops its Region, so set AWS_REGION again:

    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.txt
    

    get-caller-identity shows an ARN of the form arn:aws:sts::<account-id>:assumed-role/saa-lab-reader/lab-session, and the get-object call writes copy.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 download hello.txt.

  5. 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.txt
    

    Both fail with an AccessDenied error. Nothing in read-one-bucket allows those actions, so the role is not authorized for them and they start and stay denied. That is an implicit deny.

  6. 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.json
    

    Console: bucket → Permissions → Bucket policy → Edit, paste the policy, save.

  7. 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.txt
    

    It now fails with an AccessDenied error, even though the role's identity policy has not changed. The role's identity policy still allows s3:GetObject, but an explicit deny in any policy ends the evaluation. This is the lesson's trap "adding an Allow to override a Deny" seen from the other side. Policy changes are eventually consistent, so if the read still succeeds, wait a few seconds and retry.

  8. In terminal 1:

    aws s3api delete-bucket-policy --bucket <your-bucket-name>
    

    In terminal 2, run the same get-object command. It succeeds again, because only the identity policy's allow remains.

Verify

Run each check and compare it with the expected result.

CheckTerminalExpected result
aws sts get-caller-identity2ARN contains assumed-role/saa-lab-reader/lab-session
aws s3api get-object --bucket <your-bucket-name> --key hello.txt copy.txt2Succeeds and writes copy.txt containing secure access lab
aws s3api list-buckets2AccessDenied (implicit deny)
aws iam list-role-policies --role-name saa-lab-reader1"PolicyNames": ["read-one-bucket"]
aws iam list-attached-role-policies --role-name saa-lab-reader1"AttachedPolicies": [], so the inline policy is the role's only grant
aws s3api get-bucket-policy --bucket <your-bucket-name>1NoSuchBucketPolicy 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.

  1. Terminal 2: drop the temporary credentials, then close the window.

    unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
    
  2. 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>
    
  3. Delete the object and the bucket. --force empties the bucket first.

    aws s3 rb s3://<your-bucket-name> --force
    
  4. 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
    
  5. Delete the local files: trust.json, read.json, deny.json, hello.txt and copy.txt.

  6. 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 NoSuchEntity and the second returns a 404 (Not Found) error. Console: IAM → Roles no longer lists saa-lab-reader, and S3 → Buckets no longer lists your bucket. The lab created no other resources, so nothing from this lab should remain.

Sources

Last reviewed