Lesson 1 of 1 AWS STS

Choosing the Right AWS STS Operation for Temporary Credentials

This lesson explains how AWS Security Token Service (STS) issues temporary credentials and which operation fits each situation, which the exam expects you to choose.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between AssumeRole, AssumeRoleWithWebIdentity, AssumeRoleWithSAML and GetSessionToken from what proves the caller's identity.
  • Explain how session duration is set and limited.
  • Describe how the ExternalId and SourceIdentity parameters improve security and auditing.
  • Use aws sts get-caller-identity to confirm which identity a failing request is using.

Choosing an operation

AWS Security Token Service (STS) issues temporary credentials: an access key ID, a secret access key and a session token that expire after a set time. Which operation to call depends on what proves the caller's identity.

AssumeRole is called by a principal that already has AWS credentials and needs a role's permissions instead, for example to work in another account. It succeeds only if the caller's policy allows sts:AssumeRole on the role and the role's trust policy names the caller. A third party assuming a role in your account should also pass an external ID that the trust policy requires. AssumeRoleWithWebIdentity exchanges a token from an OpenID Connect provider, such as GitHub Actions or an EKS service account, for role credentials, so a CI/CD pipeline needs no stored access keys. AssumeRoleWithSAML does the same with a SAML assertion from a corporate identity provider.

GetSessionToken gives an IAM user temporary credentials, usually after supplying an MFA code, so that the user can call APIs whose policies require MFA. Two operations help with troubleshooting: GetCallerIdentity returns the account and ARN of the current credentials and needs no permissions, and DecodeAuthorizationMessage turns the encoded message in some authorisation failures into readable details of the denied request. The following table summarises the operations.

OperationUse it to
AssumeRoleGet credentials for a role, for cross-account access or different permissions
AssumeRoleWithWebIdentityExchange an OIDC token, such as from GitHub Actions or an EKS service account, for role credentials
AssumeRoleWithSAMLExchange a SAML assertion from a corporate identity provider for role credentials
GetSessionTokenGet temporary credentials for an IAM user, typically to call APIs that require MFA
GetCallerIdentityShow which account and principal the current credentials belong to
DecodeAuthorizationMessageDecode an encoded authorisation failure message

The following diagram matches each kind of proof of identity to the STS operation that accepts it.

Choosing an STS operation by what proves the caller's identity. A principal with AWS credentials calls AssumeRole. An OpenID Connect token, such as one from GitHub Actions or an EKS service account, is exchanged with AssumeRoleWithWebIdentity, and a SAML assertion with AssumeRoleWithSAML. An IAM user with an MFA code calls GetSessionToken. Each returns temporary credentials.
Every operation returns the same kind of temporary credentials; what differs is how the caller proves who it is.
Sort into groups

Which STS operation?

Exam skills 2.1.1 · 2.1.7

Choose the STS operation from what proves the caller's identity.

Log in to try this activity Interactive activities are free with an account.

Session duration

Temporary credentials are valid for a limited period, and short sessions limit the damage if credentials leak. For AssumeRole, the caller chooses a duration with DurationSeconds, from 15 minutes up to the role's maximum session duration, which the role's owner can set between 1 and 12 hours; the default is 1 hour. A request for longer than the maximum fails. When credentials from one role are used to assume another role, known as role chaining, the new session is limited to 1 hour regardless of the role's maximum. GetSessionToken credentials for an IAM user can last up to 36 hours. Temporary credentials cannot be revoked individually, but a policy that denies requests from sessions issued before a given time ends all existing sessions for a role. The following table summarises the durations.

SessionDuration
AssumeRole1 hour by default; 15 minutes up to the role's maximum (as long as 12 hours)
Role chaining (assuming a role with assumed-role credentials)Limited to 1 hour
GetSessionTokenUp to 36 hours

When assuming a role, a session policy can restrict permissions further, and session tags can feed policy conditions.

Fill in the value

Session durations

Exam skills 2.1.7

Recall how long STS sessions last.

Log in to try this activity Interactive activities are free with an account.

Security and auditing parameters

Additional parameters of the AssumeRole call improve security and auditing. The following list describes them.

  • ExternalId: a third party assuming a role in your account should be required, by the trust policy, to pass one. It protects against the confused deputy problem.
  • SourceIdentity: records the original user through chained sessions, so CloudTrail can attribute every action to them.

Troubleshooting access

When a script fails with AccessDenied, first confirm which identity the SDK picked up; it is often a different profile or role than expected.

aws sts get-caller-identity
# {"UserId": "AROA...:alice", "Account": "111122223333", "Arn": "arn:aws:sts::111122223333:assumed-role/Dev/alice"}

Some services, such as EC2, return an encoded message with UnauthorizedOperation. Decoding it shows the action, resource and condition that caused the denial. The caller needs sts:DecodeAuthorizationMessage.

aws sts decode-authorization-message --encoded-message "$ENCODED" \
  --query DecodedMessage --output text

To get MFA-backed credentials for the CLI, exchange long-term keys and an MFA code with GetSessionToken:

aws sts get-session-token --serial-number arn:aws:iam::111122223333:mfa/alice --token-code 123456
Worked example

A CLI command that requires MFA

A team's policy denies deleting production resources unless MFA is present. A developer runs the deletion from the CLI and is denied, although they signed in to the console with MFA.

StepCommand or result
Check identityaws sts get-caller-identity shows long-term access keys, with no MFA context
Get MFA-backed credentialsaws sts get-session-token --serial-number ... --token-code 123456
Use themExport the returned temporary credentials; the deletion now succeeds

Console MFA does not carry over to CLI access keys. In practice, IAM Identity Center sign-in (aws sso login) provides MFA-backed temporary credentials directly, and is preferred to long-term keys altogether.

Check yourself

Which STS operation does an application use to work in another AWS account?

Show answer

AssumeRole on a role in the other account whose trust policy allows the calling account or role. It returns temporary credentials for that role.

Exam Tips

Remember for the exam

  • "Which identity am I using?" is answered by aws sts get-caller-identity.
  • When API calls require MFA for an IAM user, call GetSessionToken with the MFA code.
  • To exchange an OIDC token for AWS credentials, use AssumeRoleWithWebIdentity.
  • Role chaining limits the session to one hour.
  • When a third-party vendor assumes your role, require an ExternalId in the trust policy.