Sub topic 14
Granting access across accounts
This chapter assumes you can read IAM policy JSON and know what an IAM role is. You should also know how a trust policy differs from a permissions policy, and how AWS combines Allow, Deny, and implicit deny. Here you apply those ideas when the caller and the resource sit in different AWS accounts.
Why accounts are a boundary
An AWS account is the default trust boundary. A principal (a user or role) in account A has no access to resources in account B unless account B agrees. Users in account A cannot gain access by having their own administrator write a policy, because that administrator controls only account A.
AWS gives you two ways to share access across the boundary:
| Approach | What the caller does | Whose permissions apply |
|---|---|---|
| Assume a role in the resource account | Calls `sts:AssumeRole` and receives temporary credentials | Only the role's permissions. The caller's original permissions are set aside for those calls. |
| Resource-based policy | Calls the resource directly with its own identity | The caller keeps its own permissions. The resource policy adds access. |
Use the first approach when the caller needs broad or varied access in the other account. Use the second when only a specific resource is shared and the caller needs to keep working in its own account at the same time.
Both sides must agree
For a cross-account request, an Allow in only one account is not enough. This differs from same-account access, where an Allow in either the identity-based policy or the resource-based policy can be enough.
- The account that owns the resource (the trusting account) must allow the caller.
- The caller's own account (the trusted account) must allow the caller to make the request.
The diagram shows both checks for a role-based request.
Pattern 1: assume a role in the other account
Suppose account 444455556666 owns a role named ReadOnlyAuditor. Setting it up takes three pieces.
Step 1: the trust policy on the role
The trust policy lives on the role in account 444455556666. It names who may assume the role. This version trusts account 111122223333 and requires MFA:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}The :root principal does not mean the root user. It means "the account 111122223333 as a whole". The trust policy alone does not give every identity in that account access. It lets that account's administrators decide which of their identities may assume the role.
Step 2: the caller's identity policy
In account 111122223333, attach a policy to the user or role that should be allowed to make the call:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::444455556666:role/ReadOnlyAuditor"
}
]
}If you skip this step, the request fails even though the trust policy is correct. This is the most common cross-account setup error.
Step 3: the role's permissions policy
The role in 444455556666 also needs a permissions policy that says what it can do, for example read-only actions. The trust policy answers "who may become this role". The permissions policy answers "what can this role do".
Assuming the role
aws sts assume-role \
--role-arn arn:aws:iam::444455556666:role/ReadOnlyAuditor \
--role-session-name audit-session
# The response contains a Credentials object. Export its values:
export AWS_ACCESS_KEY_ID=<AccessKeyId from the response>
export AWS_SECRET_ACCESS_KEY=<SecretAccessKey from the response>
export AWS_SESSION_TOKEN=<SessionToken from the response>
aws sts get-caller-identityThe get-caller-identity output should show an ARN like arn:aws:sts::444455556666:assumed-role/ReadOnlyAuditor/audit-session. If it still shows your original user, the exports did not take effect.
Because the trust policy above requires MFA, a caller using long-lived access keys must pass --serial-number and --token-code to assume-role. Otherwise aws:MultiFactorAuthPresent is not true and the request is denied.
Pattern 2: a resource-based policy
Some services let you attach a policy directly to the resource. The policy can name a principal from another account. The bucket policy below lets account 444455556666 read objects in a bucket:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowPartnerRead",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::444455556666:root" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
}
]
}As with trust policies, naming the account root delegates the decision. An identity in 444455556666 can read objects only if its own account also grants s3:GetObject on that bucket through an identity-based policy. The bucket policy opens the door. The partner account still decides who walks through it.
This pattern differs from role assumption in one useful way. The partner's principal keeps its own identity and permissions while it reads the bucket. Compare that with a role session, where the principal works only under the role's permissions. If a job must read the shared bucket and write to a table in its own account in the same session, a resource-based policy is simpler.
Not every service supports resource-based policies. S3 buckets, SQS queues, SNS topics, KMS keys, and Lambda functions do. Many others do not, and for those you must use a role.
Choosing between the two
- Choose a role when the caller needs many actions or resources in the other account, when you want session names in CloudTrail to show who acted, or when the service has no resource-based policy.
- Choose a resource-based policy when you share one resource, when the caller needs to keep its original permissions, or when you want to avoid managing a role.
- Choose a role whenever you need conditions on the session itself, such as MFA or a session duration limit.
Failure modes and edge cases
- Missing identity policy in the caller's account. The trust policy is correct but the caller gets
AccessDeniedonsts:AssumeRole. - Trusting a root principal too widely.
:roottrusts the entire account, including identities that were never meant to use the role. To narrow trust, name a specific role or user ARN as the principal. - Deleted principals in policies. If you name a specific user or role ARN and later delete and recreate it, the new principal has a different internal ID. The policy may no longer match it.
- Explicit deny. A Deny in any applicable policy overrides every Allow, in either account. Service control policies and permissions boundaries in the caller's account can also block the call.
- Third parties. When a vendor assumes a role in your account, add an
sts:ExternalIdcondition to the trust policy. It prevents the confused deputy problem, where the vendor is tricked into acting on your account on behalf of another customer.
Exercise
A user in account 111122223333 runs aws sts assume-role for a role in account 444455556666. The role's trust policy names account 111122223333 as the principal. The call returns AccessDenied. What is the most likely cause, and where do you fix it?
Show answer
The user's own account has not granted the call. Add an identity-based policy in account 111122223333 that allows sts:AssumeRole on the role's ARN. A trust policy alone is not enough, because cross-account access needs an Allow in both accounts. If that policy already exists, check whether the trust policy has an MFA condition the call did not satisfy, and whether an SCP or permissions boundary blocks the action.
Exercise
A bucket policy in one account names account 444455556666 as principal and allows s3:GetObject. A user in 444455556666 still cannot read the objects. What is missing?
Show answer
The user's identity-based policy in account 444455556666 must also allow s3:GetObject on the bucket's objects. For cross-account requests, the resource owner's policy and the caller's own account policy must both allow the action.