Lesson 5 of 6 AWS IAM

Granting Cross-Account Access with AWS STS and Roles

This lesson explains how temporary credentials from AWS STS power roles and cross-account access, and why they are safer than long-term keys because they expire automatically.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the STS APIs and when each is used.
  • Explain how a role grants cross-account access.
  • Choose between roles and resource-based policies.
  • Describe how AWS RAM shares resources instead of access.

STS APIs

AWS Security Token Service (STS) issues temporary credentials, which expire automatically and so limit the damage if they leak. The API used depends on what proves the caller's identity.

AssumeRole is called by a principal that already has AWS credentials, to take on a role in the same or another account; the role's trust policy must name the caller. AssumeRoleWithSAML exchanges an assertion from a SAML 2.0 identity provider, such as a corporate directory, for role credentials. AssumeRoleWithWebIdentity exchanges a token from an OpenID Connect provider, such as Cognito, Google or GitHub Actions, for role credentials. GetSessionToken gives an IAM user temporary credentials after MFA, and GetFederationToken creates a session for a federated user, typically from a custom identity broker. The following table summarises the APIs.

APIUse
AssumeRoleRoles within or across accounts
AssumeRoleWithSAMLSAML 2.0 identity providers
AssumeRoleWithWebIdentityOIDC providers: Cognito, Google, GitHub Actions
GetSessionTokenMFA-backed credentials for IAM users
GetFederationTokenFederated user sessions

Role sessions last 15 minutes to 12 hours; role chaining limits them to one hour.

Match the pairs

STS APIs

Exam skills 1.1.3 · 1.1.6

Match each caller with the STS API it uses.

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

Cross-account access with roles

Account B creates a role whose trust policy names account A; account A grants its principals sts:AssumeRole on that role's ARN. The role's permissions policy decides what they can do in account B. Third parties need an external ID in the trust policy to prevent the confused deputy problem.

{
  "Effect": "Allow",
  "Principal": {"AWS": "arn:aws:iam::111122223333:root"},
  "Action": "sts:AssumeRole",
  "Condition": {"StringEquals": {"sts:ExternalId": "vendor-7f3a"}}
}

The following diagram shows the policies on each side of a cross-account role.

Cross-account access with a role. Account B's role has a trust policy that names account A, with an external ID for a third party. Account A allows its principal sts:AssumeRole on the role's ARN. The principal assumes the role and works in account B with the permissions that the role's permissions policy grants.
Both sides must agree: account B's trust policy must name account A, and account A must allow its principal to assume the role.

Roles or resource-based policies?

Cross-account access can be granted in two ways, and they differ in what happens to the caller's own permissions. With a resource-based policy, the resource in the other account, such as an S3 bucket, an SQS queue or a KMS key, allows the caller directly. The caller keeps its own identity and permissions, so in one operation it can read from the other account's resource and write to a resource in its own account. Only services that support resource-based policies can be shared this way.

By assuming a role in the other account, the caller temporarily gives up its own permissions and acts with the role's permissions instead. This works with every service, because any action can be granted to the role, but the caller cannot use its original permissions during the session. The following table summarises the difference.

ApproachCaller's own permissionsWorks with
Resource-based policyKeptS3, SQS, SNS, KMS, Lambda, Secrets Manager and others
Assume a roleReplaced for the sessionEvery service

Copying data from a bucket in account B to a bucket in account A is simplest with a bucket policy in B, because the caller keeps its permissions in A for the write.

True or false

Roles or resource policies?

Exam skills 1.1.3 · 1.1.5

Check how the two cross-account approaches differ.

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

Sharing resources instead of access

AWS Resource Access Manager (RAM) shares resources such as subnets, transit gateways, Route 53 Resolver rules and License Manager configurations with other accounts or the organisation, so they are used in place rather than accessed through a role.

Worked example

Letting an analytics account read production data

An analytics team in account B needs read-only access to reports in a bucket in production account A. Two designs work:

Cross-account role
HowA role in A trusts account B; B's users assume it
Caller's identityTemporarily becomes A's role
Permissions neededB: sts:AssumeRole; A: role trust and permissions
Good forAccess to many resources in A
Bucket policy
HowA's bucket policy allows B's role directly
Caller's identityKeeps B's identity
Permissions neededB: IAM allow on the bucket; A: bucket policy allow
Good forAccess to one resource

Because only one bucket is involved, the bucket policy is simpler. If the analysts later needed access to several services in account A, a cross-account role would be the cleaner design. If the objects were encrypted with a KMS key, the key policy would also have to allow account B.

Check yourself

What is the purpose of an external ID when a third party assumes a role in your account?

Show answer

It prevents the confused deputy problem: the role's trust policy requires the external ID, so the third party cannot be tricked into using your role on behalf of another of its customers.

Exam Tips

Remember for the exam

  • For vendor access to your account, use a role with an external ID in the trust policy.
  • Temporary credentials come from STS and expire automatically.
  • When the caller must keep its own permissions while using another account's bucket, use a resource-based policy rather than AssumeRole.
  • Role chaining limits sessions to one hour.
  • To share subnets or transit gateways with other accounts, use AWS RAM.