AWS STS Assume Role is a service that lets you obtain temporary security credentials to access AWS resources under a different IAM role than your current one. It enables secure, time-limited access without sharing long-term credentials.
When you assume a role, AWS STS generates a set of temporary credentials-access key ID, secret access key, and session token-that expire after a configurable duration, typically between 15 minutes and 12 hours. These credentials are tied to the assumed role's permissions, not your original identity.
This capability is essential for cross-account access, federated users, and reducing privilege exposure. For example, a developer in Account A can assume a role in Account B with read-only access to S3 buckets, without needing permanent access keys.
AWS STS Assume Role operates through the AWS Security Token Service (STS), which is part of the AWS Identity and Access Management (IAM) structure. You initiate the process via an API call, AWS CLI command, or SDK request.
The workflow begins with a caller (user, role, or application) making an AssumeRole request. This request includes:
Upon approval, STS returns a JSON response containing:
AccessKeyId: Temporary access keySecretAccessKey: Secret key for signing requestsSessionToken: Session token used in all subsequent API callsExpiration: Timestamp when credentials expireThese credentials are valid only for the specified duration and cannot be refreshed. After expiration, a new AssumeRole call is required.
Assume Role is widely used in multi-account AWS environments. For example, a central DevOps team manages a shared account for logging and monitoring. Engineers in individual project accounts can assume a role in the central account to view CloudWatch logs or audit trails.
Another use case is cross-account access for third-party vendors. A company hires a security auditor who needs read access to specific S3 buckets in the production account. Instead of creating a permanent user with access, the company creates a role in the production account and grants the auditor permission to assume it, with a session limit of 1 hour.
Assume Role also supports federation. When integrating with SSO providers like Okta or Azure AD, users authenticate externally and then assume an AWS role based on their group membership. This helps access aligns with organizational policies and reduces the risk of credential leakage.
There are several forms of AssumeRole, each suited to different trust relationships:
| Method | Use Case | Authentication Required | Example |
|---|---|---|---|
| AssumeRole | Direct role assumption by IAM user or role | Valid credentials for calling principal | Developer assumes a role in another account |
| AssumeRoleWithWebIdentity | Federated access via OAuth, SAML, or OpenID Connect | JWT or SAML assertion | Mobile app user signs in via Google and assumes an AWS role |
| AssumeRoleWithSAML | Enterprise SSO integration | SAML assertion from IdP | Employee logs into corporate portal and accesses AWS |
| AssumeRoleWithExternalId | Prevents accidental cross-account access | External ID provided during trust policy setup | Vendor assumes a role in your account with a unique external ID |
Each variant enforces additional trust checks. For example, AssumeRoleWithWebIdentity requires the identity provider to sign the token, ensuring it wasn't forged.
One critical mistake is assuming roles with excessive permissions. A role granted full admin access should never be assumed by untrusted identities. Always follow the principle of least privilege-grant only the minimum permissions needed.
Another issue is credential reuse. Temporary credentials from AssumeRole should never be stored in configuration files, environment variables, or source code. They are meant for short-lived sessions. Storing them increases the risk of exposure.
Non-obvious fact: AWS STS AssumeRole does not require the caller to have permission to modify the role's trust policy. The trust policy defines who can assume the role, not who can use it. If a user can assume a role, they gain all permissions defined in that role's policy-even if they don't have direct access to the role itself.
For example, if a user in Account A assumes a role in Account B, and that role allows full access to EC2 instances in Account B, the user gains those permissions-even if they were not explicitly granted access to the role resource.
Consider a company with two AWS accounts: prod-123456 (production) and dev-789012 (development).
A developer in the dev account needs to deploy a Lambda function to the prod account. Instead of sharing the root user password or creating a long-term access key, the team sets up a role called DeployerRole in the prod account with the following trust policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::789012:role/DevTeam"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": "deploy-prod-789"
}
}
}
]
}
The developer runs the following AWS CLI command:
aws sts assume-role \
--role-arn arn:aws:iam::123456:role/DeployerRole \
--role-session-name deploy-session-2024-04-05 \
--duration-seconds 3600 \
--external-id deploy-prod-789
STS responds with temporary credentials. The developer then configures the AWS CLI to use these credentials and deploys the Lambda function. After one hour, the session ends automatically. No long-term keys were created.
Assume Role is not a replacement for IAM policies. It doesn't change the underlying permissions of the role-it only provides a way to access them securely. You still need to define what actions the role can perform.
It's also not a substitute for proper authentication. AssumeRole requires a valid identity to initiate the call. If someone compromises your AWS access key, they can assume roles you're authorized to assume-so protecting your long-term credentials remains critical.
Finally, AssumeRole does not support delegation chains beyond one hop. You cannot assume a role, then use those credentials to assume another role unless explicitly configured. This prevents unintended privilege escalation.
Always set a maximum session duration. The default is 3600 seconds (1 hour), but you can reduce it to 15 minutes for high-risk operations. Use the --duration-seconds flag in CLI commands or DurationSeconds parameter in API calls.
Require external IDs when granting cross-account access. This prevents attackers from assuming your role if they guess the ARN. External IDs act as a shared secret between accounts.
Use session names that include context-like deploy-session-john-2024-04-05-to track who assumed the role and when. This improves auditing and incident response.
Firsthand observation: During a penetration test, we discovered a misconfigured AssumeRole policy that allowed any user in a trusted account to assume a role with full S3 access. The fix was to restrict the Principal to specific IAM roles and enforce external IDs. This reduced the attack surface.