All guides

AWS STS and AssumeRole: how short-lived credentials work

An IAM role has no long-term credentials such as a password or access keys. A principal that is allowed to assume the role receives temporary security credentials from AWS STS, valid for one hour by default.

Hands typing on a laptop that shows code

Roles and temporary credentials

An IAM role is an identity with specific permissions and no long-term credentials. When a principal assumes it, the AWS Security Token Service (STS) hands back three temporary values: an access key ID, a secret access key and a security token.

AWS lists who can assume a role: an IAM user in the same account or another account, IAM roles in the same account, AWS services, and an external user authenticated by a SAML 2.0 or OpenID Connect identity provider.

Two policies on every role

  • The trust policy specifies who can assume the role. It acts as a resource-based policy on the role, and it names the trusted principals.
  • The permissions policy specifies what can be done with the role.

To assume a role in a different account, two things are needed. The role's trust policy must trust the caller's account, and an administrator of that account must attach a policy that allows the caller to call AssumeRole for the role's ARN. Within one account, either one is enough: a policy on the user that allows the call, or the user named as a principal in the trust policy.

An account can be written in a trust policy as arn:aws:iam::111122223333:root or as the 12-digit account ID. AWS states that both behave the same way and delegate to the account. The ARN form does not limit the role to the account's root user.

An example trust policy

This is AWS's example for a third party, with the account ID changed. It lets the account 111122223333 assume the role only when the call includes the external ID 12345, the value AWS uses in its example:

JSON

{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Principal": {
      "AWS": "111122223333"
    },
    "Action": "sts:AssumeRole",
    "Condition": {
      "StringEquals": {
        "sts:ExternalId": "12345"
      }
    }
  }
}

The AssumeRole call

--role-arn and --role-session-name are required. --external-id and --duration-seconds are optional flags of the same command:

Shell

aws sts assume-role \
  --role-arn arn:aws:iam::444455556666:role/deploy \
  --role-session-name my-session \
  --external-id 12345 \
  --duration-seconds 3600

The output has a Credentials block with AccessKeyId, SecretAccessKey, SessionToken and Expiration. The session name becomes part of the ARN of the role session, in the form arn:aws:sts::444455556666:assumed-role/deploy/my-session.

The CLI can also assume a role from a profile. This is AWS's example:

INI

[profile marketingadmin]
role_arn = arn:aws:iam::123456789012:role/marketingadminrole
source_profile = user1

A command run with --profile marketingadmin uses the credentials of the user1 profile to assume the role. Add external_id to the role profile when the role needs an external ID. Add role_session_name to set the session name, which is also included in the CloudTrail logs.

Session duration

  • With no DurationSeconds, the session lasts 3600 seconds, one hour.
  • The minimum is 900 seconds, 15 minutes.
  • The maximum is the role's maximum session duration. The setting accepts 1 hour to 12 hours and defaults to 1 hour.
  • If you request more than the role's maximum, the operation fails.

The maximum is changed with update-role. The value is in seconds, from 3600 to 43200; this example sets four hours, so put in the number your role needs:

Shell

aws iam update-role --role-name deploy --max-session-duration 14400

The change takes effect the next time someone assumes the role. For sessions that already exist, IAM can revoke all permissions of a role's credentials issued before a point in time. AWS does this by attaching an inline policy to the role that denies all actions, and the policy applies to all users of that role.

The external ID for third parties

AWS describes the risk with a third party that assumes roles for many customers. A second customer gives the third party the ARN of your role. The third party then uses your role on that customer's behalf. AWS calls this the confused deputy problem.

The external ID addresses it. The third party generates a unique ID for each customer. You put that ID in the trust policy under sts:ExternalId, and the third party includes it in every AssumeRole call. Another customer who supplies your role's ARN cannot control the external ID that the third party sends.

Two points from the same page:

  • AWS handles the external ID as a value that is not secret. Anyone with permission to view the role can see it.
  • The third party must generate it, to make sure each external ID is unique.

Role chaining and its limit

Role chaining means assuming one role and then using it to assume another.

Role chaining limits the role session to a maximum of one hour, regardless of the maximum session duration configured for the individual roles. If a chained AssumeRole call provides a DurationSeconds value greater than one hour, the operation fails. On the second call, request one hour or less.

What to check on existing roles

  1. Account principals. When a trust policy names another account, an administrator in that account decides which of its identities get access. Confirm you intend that.
  2. Third-party roles. AWS says to use an external ID when the third party accesses other AWS accounts in addition to yours. IAM Access Analyzer shows whether principals outside your organization or account can assume your roles.
  3. Maximum durations. With a long session, a party that obtains exposed credentials has access for the duration of the session.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides