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.
| API | Use |
|---|---|
AssumeRole | Roles within or across accounts |
AssumeRoleWithSAML | SAML 2.0 identity providers |
AssumeRoleWithWebIdentity | OIDC providers: Cognito, Google, GitHub Actions |
GetSessionToken | MFA-backed credentials for IAM users |
GetFederationToken | Federated user sessions |
Role sessions last 15 minutes to 12 hours; role chaining limits them to one hour.
STS APIs
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.

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.
| Approach | Caller's own permissions | Works with |
|---|---|---|
| Resource-based policy | Kept | S3, SQS, SNS, KMS, Lambda, Secrets Manager and others |
| Assume a role | Replaced for the session | Every 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.
Roles or resource policies?
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.
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:
sts:AssumeRole; A: role trust and permissionsBecause 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.
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.