← Solutions Architect Associate study path

Secure architectures · 12 min

Cross-account access with roles and AWS STS

In this lesson you will learn how AWS evaluates a request that crosses from one account to another, when to grant that access with a role and when with a resource policy, and how the ExternalId prevents the confused deputy problem.

Many companies use several AWS accounts, one for each team or each stage of their work, so that a mistake in one doesn't affect the others. Cross-account access is the case where an identity in one AWS account uses a resource that is in another account. For example, an online store has a development account, where the programmers test changes, and a account; a development role that reads a in the production account is cross-account access, and so is an external vendor that reviews the store's costs.

On the Solutions Architect exam this topic shows up in design questions, where you choose how to grant that access, and in troubleshooting questions, where you explain why a request gets AccessDenied, which is the error AWS returns when it denies something. Both are solved with the same evaluation rule.

How does AWS evaluate a cross-account request?

A request is anything someone asks AWS to do, such as reading a file, and a policy is a document with rules that say Allow or Deny. Two types of policies take part in a request to another account. An identity policy is attached to a user, group or role and says what that identity can do, while a resource policy is attached to the resource, such as an S3 bucket, and says who can use it. For example, the identity policy of the store's development role says "can read buckets", and the policy of the production photo bucket says "the development role can read here".

Within a single account it is enough for one of the two policies to allow the action. However, when the request crosses to another account, AWS evaluates it twice: in the requester's account it checks the identity policy, and in the account that owns the resource it checks the resource policy. The request is allowed only if both evaluations result in Allow, and an explicit Deny in either of them denies it.

That rule solves most cross-account access questions. For example, the policy of a bucket in account B allows the dev role in account A to read, but the dev role has no S3 policy at all. In that case the role can't read the bucket, because the Allow on A's side is missing.

True or false?

0 of 3
  1. If a bucket policy allows a role from another account, that role can already read even if its identity policy doesn't mention S3.

  2. Within the same account, a bucket policy that names a user is enough for her to read.

  3. An explicit Deny in the requester's account blocks access even if the owning account allows it.

A role in the other account

The main way to grant cross-account access is a role in the account that owns the resource. A role is a set of permissions with no password that someone borrows for a while, and taking it is called assuming the role. Whoever assumes that role with the AssumeRole call of AWS Security Token Service (AWS STS), the service that hands out permissions for a limited time, receives temporary and works with the role's permissions. For example, one of the store's programmers assumes a role in the production account from the development account, works for an hour and the credentials expire on their own.

Account A assumes a role in account B with AssumeRole; the role has a trust policy toward A and a permissions policy to read a bucket

The role in account B, the trusting account, has two policies. The trust policy says who can assume it, in this case account A, and the permissions policy says what whoever assumes it can do, in this case read the bucket. On A's side, the development role needs its own identity policy that allows it sts:AssumeRole on the of B's role, because here too both evaluations are needed.

The access happens in three steps: A calls AssumeRole, AWS STS checks the trust policy in B and returns temporary credentials, and A uses them to read the bucket. While it uses those credentials, A works with the permissions of B's role and no longer has its own. For example, while the store's programmer works with the production role, they can't touch the resources in the development account with that same session, which is the time those credentials last.

Role or resource policy?

If the service has resource policies, like S3, SQS (message queues), SNS (sending notifications) or KMS (keys for encryption), there is a second path: putting the principal from the other account directly in the resource policy. The two paths differ in what happens to the permissions the principal has in its own account:

  • The role, which makes whoever assumes it give up their original permissions while they use that session.
  • The resource policy, where the principal is still itself and keeps its permissions in its account.

That difference decides the cases where an identity needs permissions in both accounts at the same time. For example, if an identity has to copy from a bucket in account A to one in account B, the resource policy is the better fit, because that identity needs to read in A and write in B in the same operation. On the other hand, if the service has no resource policies, the only option is the role.

Role or resource policy?

A role in account A has to copy objects from a bucket in account A to a bucket in account B in a single operation. Which design works?

The confused deputy problem

The confused deputy shows up when you grant access with a role to a vendor that serves many customers. The IAM guide explains it with a scenario: you hire Example Corp, a vendor that reviews costs, create a role that trusts its account and give it the role's ARN. Because the ARN is not a secret, another Example Corp customer can find it out and register it as if it were theirs, and then Example Corp assumes your role and shows your data to that other customer. Example Corp is the "deputy" in this case, because it has the permission and someone tricked it into using it.

A vendor with several customers assumes roles in their accounts; the ExternalId in the trust policy condition stops one customer from using another's role

The solution is a condition in the trust policy, which is a rule that only lets someone assume the role if something is true, and that condition is the ExternalId. Example Corp generates a unique value per customer, you put it as an sts:ExternalId condition in the trust policy and Example Corp sends it every time it assumes the role. If another customer gives your ARN, Example Corp sends that other customer's ExternalId, the value doesn't match and AssumeRole fails. For example, if the store hires a vendor that reviews costs and the vendor assigns it an ExternalId, another customer of the vendor who registers the ARN of the store's role gets an error and sees nothing of the store's.

This value has two details the exam can ask about: the ExternalId is generated by the third party and not by you, and AWS doesn't treat it as a secret. Also, when whoever accesses your resource is an AWS service and not another account, the same problem is closed in the resource policy with the aws:SourceArn, aws:SourceAccount or aws:SourceOrgID conditions, which check which resource, account or organization the service's request comes from. For example, if an AWS service writes logs to one of the store's buckets, the aws:SourceAccount condition makes sure it can only do so when it is working for the store's account and not for another company's.

Match each piece with what it does

0 of 4 pairs

Tap a piece and then its function.

0 of 4 pairs.

In a new experience account

A new experience account is the one you open with Google, GitHub, Apple, Amazon or email, and in it AWS puts an RCP in every project that can't be modified. An RCP is a policy of AWS Organizations, the service that groups the accounts of a single organization, and it sets a maximum on what can be done with the resources. That policy denies actions on STS, S3, KMS, Secrets Manager, SQS, DynamoDB and other services to any principal outside your organization, so an external auditor can't assume a role in your project.

Between two projects of the same owner you can grant access, because both are in the same organization. For example, you can practice the store's case with a development project and a production project, but you can't let the cost vendor in. To practice third-party access, with its ExternalId, you need a full account.

How does it show up on the exam?

Cross-account access questions usually describe who needs to get in and for how long, and these phrases in the question point to the answer:

If the question says The answer
"A SaaS company needs to access the accounts of hundreds of customers" A role in each customer account that trusts the SaaS account, with a unique ExternalId per customer
"External auditor, temporary access" A cross-account role
"The role in the other account has the permission but gets AccessDenied" It is missing the Allow in its own identity policy, or there is a Deny

A company is a vendor that serves many customers with the same system, and that is why it is the typical confused deputy case. For example, if the question says the store's cost vendor serves hundreds of companies, the answer involves a role in the store's account with a unique ExternalId.

Review question

A company hires a monitoring vendor that needs to read metrics from CloudWatch, the service that measures how your resources are working, in its account. The vendor serves hundreds of customers from a single AWS account. Which configuration gives it access and prevents another customer of the vendor from getting the company's data?

Next

  • The Security best practices in IAM, in the AWS documentation, especially the section on public and cross-account access with IAM Access Analyzer.
  • The Solutions Architect practice exams at /examenes.