Learn/Security
Security · 14 min read · August 2026
STS AssumeRole: Stop Using Long-Lived Keys
Temporary credentials via STS AssumeRole — the pattern AWS documents for humans, CI, and cross-account labs instead of copying access keys into laptops.
Source. AWS STS AssumeRole API · IAM Roles · Temporary security credentials
Abstract & theoretical context
AWS Security Token Service (STS) issues short-lived credentials. AssumeRole returns AccessKeyId, SecretAccessKey, and SessionToken bound to a role’s policy. That is how consoles, AWS CLI profiles, and GitHub Actions should enter an account. Long-lived IAM user keys on a student laptop fail the least-privilege story the moment the laptop is lost. This lab reconstructs the official AssumeRole path: a source identity, a destination role, an external ID or MFA condition when you are ready, and aws sts get-caller-identity as the proof.
Models, formulas & variables
1. Session
Creds(t) = STS.AssumeRole(RoleArn, DurationSeconds) where t < DurationSeconds ≤ 43200
- — Default max session is often 1 hour unless the role allows longer.
- — Session policies can further restrict the role for that call only.
2. Trust vs permissions
Trust policy = who may assume. Permissions policy = what the role can do after assume.
- — A correct trust policy with a wild permissions policy is still a wide role.
- — Principal in trust must be the user, role, or account that calls AssumeRole.
Reference implementation
Copy into a sandbox account. Pin Region ap-south-1 unless the lab says otherwise.
assume-lab-role.sh
AWS CLI · bash
aws sts get-caller-identity
aws sts assume-role \
--role-arn arn:aws:iam::ACCOUNT:role/SbgLabReader \
--role-session-name tcet-builder-$(date +%s) \
--duration-seconds 900
# Export AccessKeyId, SecretAccessKey, SessionToken from the JSON, then:
aws sts get-caller-identity
aws s3 ls
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKENLab procedure
- 01Create role SbgLabReader with a trust policy for your IAM user and a read-only permissions policy.
- 02From the CLI as yourself, run get-caller-identity and note the ARN.
- 03Assume the role, export the three keys, run get-caller-identity again — the ARN must change to the role.
- 04Prove the role can list what you allowed and is denied on what you did not.
- 05Unset the keys. Confirm you are yourself again.
Key insights
- If get-caller-identity still shows the user, the env vars did not stick.
- Never paste assumed keys into chat or GitHub.
- CI should use OIDC to AssumeRole, not a user access key in secrets.
Account & lab requirements
IAM user that can iam:PassRole is not required for self-assume; you need iam:CreateRole in a sandbox or a mentor-provisioned role.