Sub topic 14
Granting access across accounts
This chapter assumes you can read IAM policy JSON, know what a role's trust policy is, and have used aws sts assume-role. Here you will compare the two ways to share access between AWS accounts and see when each one fits.
Two ways to cross an account boundary
An identity in account A cannot use resources in account B unless account B allows it. You have two options:
- Assume a role in the account that owns the resource. The caller gives up its original permissions and works as the role.
- Attach a resource-based policy to the resource that names the outside principal. The caller keeps its own identity.
Option 1: assume a role
Account B creates a role. The role needs two policies:
- A trust policy that names who may assume it.
- A permissions policy that says what the role can do.
This trust policy lets principals in account 111122223333 assume the role, but only when they signed in with MFA:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}Naming the account root as the principal delegates the decision to account 111122223333. It does not give every identity in that account access. Each identity there still needs its own identity-based policy that allows sts:AssumeRole on the role's ARN.
The caller then assumes the role and exports the temporary credentials:
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-identityget-caller-identity should now show an assumed-role ARN in account 444455556666. That confirms later calls run as the role.
Option 2: a resource-based policy
Some services let you attach a policy to the resource. This bucket policy names a principal in another account:
{
"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/*"
}
]
}The bucket owner has allowed access. Account 444455556666 must also allow its own identities to call s3:GetObject on that bucket. For a request across accounts, both sides must allow it.
Choosing between them
| Question | Assume a role | Resource-based policy |
|---|---|---|
| Caller's own permissions during the call | Replaced by the role's | Kept |
| Works for every service | Yes | Only services that support resource policies |
| Credentials | Temporary, from STS | The caller's existing ones |
| Good for | Broad or multi-service access | Sharing one resource |
Prefer a resource-based policy when the caller needs to keep working in its own account, for example copying from a partner's bucket into its own.
Exercise
Account 444455556666 has a bucket policy allowing account 111122223333 to call s3:GetObject. A user in 111122223333 still gets AccessDenied. What is the likely cause?
Show answer
The user's own account must also allow the action. The user needs an identity-based policy that allows s3:GetObject on the bucket's objects. For cross-account requests, both the resource policy and the caller's identity policy must allow the request.