IAM for Developers: Roles, Policies, STS AssumeRole, Permission Boundaries and Least Privilege
The Building Blocks
| Item | Meaning | Developer use |
| User | Long-term identity for a person or app | Avoid for apps. Use roles |
| Group | Set of users sharing permissions | Attach policies to groups |
| Role | Identity assumed for temporary credentials. Has a trust policy (who may assume) and permission policies | EC2, Lambda, ECS, cross-account, CI/CD |
| Policy | JSON document of allow/deny statements | Defines permissions |
| Instance profile | Container that passes a role to EC2 | Give 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"}}
}
]
}
| Element | Meaning |
Effect | Allow or Deny |
Action | API calls such as s3:GetObject (wildcards allowed). NotAction means everything except |
Resource | ARNs the statement covers. Some actions need * |
Principal | Who the policy applies to. Used in resource policies and trust policies, not in identity policies |
Condition | Extra tests: IP, MFA, tags, Region, VPC endpoint, time |
Identity-Based vs Resource-Based Policies
| Type | Attached to | Examples |
| Identity-based | User, group or role | AmazonS3ReadOnlyAccess, your custom policy |
| Resource-based | The resource | S3 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
- Everything is denied by default
- Evaluate all policies. Any explicit Deny wins immediately
- Organizations SCPs and permission boundaries and session policies must also allow (they only limit)
- Then an Allow in an identity or resource policy grants access
- 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 call | Use |
AssumeRole | Same or cross-account role |
AssumeRoleWithWebIdentity | OIDC tokens (EKS, GitHub Actions, Cognito) |
AssumeRoleWithSAML | Enterprise SAML federation |
GetSessionToken | Temporary credentials for a user, for example with MFA |
GetCallerIdentity | Who am I? Needs no permission |
DecodeAuthorizationMessage | Decode 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
- Run
aws sts get-caller-identity and confirm which identity is calling
- Read the error text. It names the action and resource
- Check for explicit Deny in identity policy, bucket policy, SCP, boundary or session policy
- For KMS-encrypted data check the key policy and
kms:Decrypt
- Check the Region and account of the resource
- Use the Policy Simulator or CloudTrail (the event shows
errorCode and errorMessage)
- Decode long messages with
aws sts decode-authorization-message --encoded-message ...
Practice Tasks
- Create a role that a Lambda function can assume and allow only
dynamodb:GetItem on one table
- Write a trust policy so that another account can assume the role with an external ID
- Use
assume_role in boto3 and list a bucket with the temporary keys
- Create a permission boundary and prove that a role cannot exceed it
- 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.