IAM for Developers: Roles, Policies, STS AssumeRole, Permission Boundaries and Least Privilege

AWS Developer Associate 50 min min read Updated: Nov 17, 2026 Intermediate
IAM for Developers: Roles, Policies, STS AssumeRole, Permission Boundaries and Least Privilege
Intermediate Topic 2 of 3

The Building Blocks

ItemMeaningDeveloper use
UserLong-term identity for a person or appAvoid for apps. Use roles
GroupSet of users sharing permissionsAttach policies to groups
RoleIdentity assumed for temporary credentials. Has a trust policy (who may assume) and permission policiesEC2, Lambda, ECS, cross-account, CI/CD
PolicyJSON document of allow/deny statementsDefines permissions
Instance profileContainer that passes a role to EC2Give an instance S3 access

Anatomy of a Policy

json { "Version": "2012-10-17", "Statement": [ { "Sid": "ReadOrdersTable", "Effect": "Allow", "Action": ["dynamodb:GetItem", "dynamodb:Query"], "Resource": "arn:aws:dynamodb:ap-south-1:111111111111:table/Orders", "Condition": {"StringEquals": {"aws:RequestedRegion": "ap-south-1"}} } ] }
ElementMeaning
EffectAllow or Deny
ActionAPI calls such as s3:GetObject (wildcards allowed). NotAction means everything except
ResourceARNs the statement covers. Some actions need *
PrincipalWho the policy applies to. Used in resource policies and trust policies, not in identity policies
ConditionExtra tests: IP, MFA, tags, Region, VPC endpoint, time

Identity-Based vs Resource-Based Policies

TypeAttached toExamples
Identity-basedUser, group or roleAmazonS3ReadOnlyAccess, your custom policy
Resource-basedThe resourceS3 bucket policy, SQS queue policy, Lambda resource policy, KMS key policy, role trust policy
Same account vs cross-account: Inside one account either an identity policy or a resource policy that allows is enough (if nothing denies). Across accounts both sides must allow: the resource policy in the owner account and an identity policy in the caller account.

Policy Evaluation Logic

  1. Everything is denied by default
  2. Evaluate all policies. Any explicit Deny wins immediately
  3. Organizations SCPs and permission boundaries and session policies must also allow (they only limit)
  4. Then an Allow in an identity or resource policy grants access
  5. Otherwise the request is denied

Roles and AssumeRole

A role has two policies. The trust policy says who can assume it. The permissions policy says what it can do.

json { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": {"AWS": "arn:aws:iam::111111111111:role/CiDeployer"}, "Action": "sts:AssumeRole", "Condition": {"StringEquals": {"sts:ExternalId": "partner-7781"}} }] }
python import boto3 sts = boto3.client("sts") r = sts.assume_role( RoleArn="arn:aws:iam::222222222222:role/ReadOnly", RoleSessionName="report-job", DurationSeconds=3600, ) c = r["Credentials"] # AccessKeyId, SecretAccessKey, SessionToken, Expiration s3 = boto3.client( "s3", aws_access_key_id=c["AccessKeyId"], aws_secret_access_key=c["SecretAccessKey"], aws_session_token=c["SessionToken"], ) print(s3.list_buckets()["Buckets"][:2])
STS callUse
AssumeRoleSame or cross-account role
AssumeRoleWithWebIdentityOIDC tokens (EKS, GitHub Actions, Cognito)
AssumeRoleWithSAMLEnterprise SAML federation
GetSessionTokenTemporary credentials for a user, for example with MFA
GetCallerIdentityWho am I? Needs no permission
DecodeAuthorizationMessageDecode an encoded authorization failure message

Permission Boundaries and Session Policies

  • A permission boundary is a policy that sets the maximum permissions a user or role can ever have. Effective access is the intersection of the boundary and the identity policy
  • Use it to let developers create roles for their apps without being able to grant more power than they have
  • A session policy passed to AssumeRole limits a single session further

Least Privilege in Practice

  • Start with what the code needs: list its API calls (CloudTrail helps)
  • Scope resources to specific ARNs, not *
  • Add conditions such as source VPC endpoint, tags or MFA
  • Use IAM Access Analyzer to generate a policy from CloudTrail activity and to find unused access
  • Test with the IAM Policy Simulator
  • Prefer AWS managed policies for learning, customer managed policies for production, and avoid inline policies except for strict one-to-one cases
json { "Effect": "Allow", "Action": ["s3:GetObject", "s3:PutObject"], "Resource": "arn:aws:s3:::my-app-uploads/${aws:PrincipalTag/team}/*" }

The ${aws:PrincipalTag/team} variable gives each team only its own prefix (attribute-based access control, or ABAC), with one policy for many teams.

Debugging AccessDenied

  1. Run aws sts get-caller-identity and confirm which identity is calling
  2. Read the error text. It names the action and resource
  3. Check for explicit Deny in identity policy, bucket policy, SCP, boundary or session policy
  4. For KMS-encrypted data check the key policy and kms:Decrypt
  5. Check the Region and account of the resource
  6. Use the Policy Simulator or CloudTrail (the event shows errorCode and errorMessage)
  7. Decode long messages with aws sts decode-authorization-message --encoded-message ...

Practice Tasks

  1. Create a role that a Lambda function can assume and allow only dynamodb:GetItem on one table
  2. Write a trust policy so that another account can assume the role with an external ID
  3. Use assume_role in boto3 and list a bucket with the temporary keys
  4. Create a permission boundary and prove that a role cannot exceed it
  5. Cause an AccessDenied on purpose and find it in CloudTrail

Conclusion

Use roles with temporary credentials, write least-privilege JSON policies with specific resources and conditions, remember that explicit deny wins and cross-account needs both sides, use permission boundaries to delegate safely, and debug AccessDenied with caller identity, policy simulator and CloudTrail.

Quick Summary: building blocks; policy anatomy; identity vs resource; evaluation; roles/AssumeRole; boundaries; least privilege; debugging.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators