SkillSprout
Chapter 8 · Lesson 1
Intermediate
6 min read

IAM Policies, Roles and STS

Overview

AWS Identity and Access Management (IAM) decides whether each API call is allowed. As a developer you write the policies that give applications just enough power to do their job, and use STS to hand out short-lived credentials when one identity needs to act as another.

Think of an office building with badge readers. A policy is the rule on a badge: which doors it opens and when. A role is a badge you borrow for a shift; STS is the security desk that issues it and takes it back automatically. Least privilege means giving the cleaner a badge for the cleaning cupboard, not the server room.

Policy reading is a huge part of DVA-C02: you must evaluate allow/deny logic, understand conditions, and know the difference between identity-based, resource-based and boundary policies.

Key Concepts

  • Identity-based policies attach to users, groups and roles; resource-based policies attach to resources (S3, SQS, Lambda, KMS keys) and can grant cross-account access directly.
  • Evaluation: explicit Deny always wins; otherwise need an explicit Allow; the default is implicit deny.
  • Permissions boundaries and SCPs (Organizations) set a maximum; they never grant by themselves, they only limit.
  • Policy elements: Effect, Action, Resource, Principal (resource policies), Condition, and policy variables like ${aws:username}.
  • STS operations: AssumeRole, AssumeRoleWithWebIdentity, AssumeRoleWithSAML, GetSessionToken, GetCallerIdentity; credentials last 15 minutes to 12 hours.
  • A role's trust policy controls who can assume it; sts:ExternalId prevents the confused deputy problem for third parties.
  • iam:PassRole is required for a user to give a role to a service (for example attaching a role to a Lambda function), so restrict it.
  • IAM Access Analyzer finds external access and validates policies; last-accessed data helps remove unused permissions.

Detailed Notes

Anatomy of a policy

json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ReadOwnPrefix",
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject"],
      "Resource": "arn:aws:s3:::app-data/home/${aws:userid}/*"
    },
    {
      "Sid": "DenyUnencryptedTransport",
      "Effect": "Deny",
      "Action": "s3:*",
      "Resource": ["arn:aws:s3:::app-data", "arn:aws:s3:::app-data/*"],
      "Condition": { "Bool": { "aws:SecureTransport": "false" } }
    }
  ]
}

Evaluation logic

  1. Start with implicit deny.
  2. Any explicit Deny in any applicable policy ends evaluation with deny.
  3. For same-account access, an Allow in an identity or resource policy (within boundary/SCP limits) permits the call.
  4. For cross-account access, both the identity policy in the caller's account and the resource policy (or an assumed role) in the target account must allow.
Policy typeGrants?Limits?
Identity-basedYes-
Resource-basedYes (including cross-account principals)-
Permissions boundaryNoYes, on a user/role
SCPNoYes, on account/OU
Session policyNoYes, on a session

Useful condition keys

KeyUse
aws:SourceIp, aws:SourceVpceRestrict by network
aws:RequestedRegionRegion restriction
aws:PrincipalTag/, aws:ResourceTag/Attribute-based access control (ABAC)
aws:MultiFactorAuthPresentRequire MFA
aws:PrincipalOrgIDLimit to your organization
s3:x-amz-server-side-encryptionRequire encryption on upload

Assuming a role with STS

bash
aws sts assume-role \
  --role-arn arn:aws:iam::222233334444:role/DeployRole \
  --role-session-name ci-deploy \
  --external-id 7f3a-example

aws sts get-caller-identity
python
import boto3
sts = boto3.client("sts")
creds = sts.assume_role(
    RoleArn="arn:aws:iam::222233334444:role/DeployRole",
    RoleSessionName="ci-deploy",
    DurationSeconds=1800,
    Policy='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}',
)["Credentials"]

The inline session Policy further restricts the role's permissions for that session.

Trust policy for cross-account

json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "AWS": "arn:aws:iam::111122223333:root" },
    "Action": "sts:AssumeRole",
    "Condition": { "StringEquals": { "sts:ExternalId": "7f3a-example" } }
  }]
}

The caller also needs sts:AssumeRole on that role in their own identity policy.

Lambda resource-based policy

bash
aws lambda add-permission --function-name OrdersFn \
  --statement-id s3invoke --action lambda:InvokeFunction \
  --principal s3.amazonaws.com --source-arn arn:aws:s3:::uploads \
  --source-account 111122223333

Event sources that push (S3, SNS, API Gateway) need a resource-based permission on the function. Pollers (SQS, Kinesis) use the function's execution role instead.

Debugging AccessDenied

  1. aws sts get-caller-identity -> confirm the principal.
  2. Check for explicit denies (SCP, boundary, resource policy, bucket policy).
  3. Decode the message with sts decode-authorization-message.
  4. Use the IAM Policy Simulator and CloudTrail to see the failing request.
  5. Check KMS key policy if encryption is involved.

Least privilege workflow

Start with a narrow policy, run the app, read Access Analyzer policy generation from CloudTrail, and tighten. Prefer resource ARNs over *, specific actions over s3:*.

Policy evaluation and condition keys

RuleEffect
DefaultImplicit deny
Explicit DenyAlways wins
AllowNeeded in an identity or resource policy (same account)
Permissions boundary / SCPCap the maximum permissions; never grant by themselves
Cross-accountNeeds an Allow on both sides (identity policy and resource or role trust policy)
json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:GetObject"],
    "Resource": "arn:aws:s3:::orders-data/${aws:PrincipalTag/team}/*",
    "Condition": { "Bool": { "aws:SecureTransport": "true" },
                   "StringEquals": { "aws:RequestedRegion": "eu-west-1" } }
  }]
}

STS calls you should know

python
import boto3
sts = boto3.client("sts")
c = sts.assume_role(RoleArn="arn:aws:iam::222233334444:role/reader",
                    RoleSessionName="job-42", DurationSeconds=3600,
                    ExternalId="partner-123")["Credentials"]
s3 = boto3.client("s3", aws_access_key_id=c["AccessKeyId"],
                  aws_secret_access_key=c["SecretAccessKey"], aws_session_token=c["SessionToken"])

Related APIs: AssumeRoleWithWebIdentity (OIDC tokens), AssumeRoleWithSAML, GetSessionToken (MFA) and aws sts decode-authorization-message to read an encoded authorisation failure. Use an external ID for third-party cross-account roles to prevent the confused-deputy problem, and iam:PassRole to control which roles a user can hand to a service.

Real World Example

A CI/CD platform team needs build jobs in a tooling account to deploy into production accounts. Each production account has a DeployRole whose trust policy trusts only the pipeline role with an ExternalId and an MFA-free but org-scoped condition (aws:PrincipalOrgID). Developers can only call iam:PassRole for roles with a specific path and tag, and an SCP denies leaving approved Regions. When a deploy fails with AccessDenied, engineers decode the message and discover the KMS key policy lacked the role.

Architecture Diagram

Cross-account role assumption
Architecture
AWS
Tooling account
Pipeline role
STS
Production account
DeployRoletrust policy
Stack / resources

The caller assumes a role in another account; STS returns temporary credentials limited by policies.

  1. 1The pipeline role calls sts:AssumeRole on the target account's role.
  2. 2The target role's trust policy and any conditions are checked.
  3. 3STS returns temporary credentials limited by the role's permissions and boundaries.
  4. 4The pipeline uses them to deploy; CloudTrail records the session name.

Best Practices

  • ✓Grant least privilege: specific actions and resource ARNs.
  • ✓Use roles and STS rather than long-lived access keys.
  • ✓Add conditions (MFA, source VPC, org ID, tags) where relevant.
  • ✓Restrict iam:PassRole and use permissions boundaries for delegated admins.
  • ✓Review access with Access Analyzer and last-accessed data.

Common Interview Questions

Exam Tips

DVA-C02 focus

  • Explicit Deny overrides everything.
  • Cross-account via resource policy: both sides must allow unless the principal assumes a role.
  • Service pushing to another service -> resource-based policy; polling -> execution role.
  • Decode failures with sts decode-authorization-message.

Finished this lesson?

0 of 46 lessons done · 0% of the course