Granting Explicit and Auditable Cross-Account Access
This lesson explains the two fundamental mechanisms for cross-account access, role assumption and resource-based policies, and how the choice depends on whose permissions should apply.
Learning outcomes
After completing this lesson, you will be able to:
- Choose between role-based and resource-based cross-account access.
- Explain why both sides must allow a cross-account request.
- Explain how external IDs prevent the confused deputy problem.
- Explain what cross-account access to encrypted resources requires.
- Describe how IAM Access Analyzer audits cross-account access.
Two ways to grant cross-account access
There are two ways for a principal in one account to use resources in another. With role-based access, a role in the target account trusts the source account, and source principals call sts:AssumeRole. While using the role, the caller temporarily gives up its own permissions and has only the role's. This suits administering or deploying into another account. When a third party assumes the role, the trust policy should require an external ID to prevent the confused deputy problem.
With resource-based access, a policy on the resource itself, on S3, KMS, SQS, SNS, Lambda, Secrets Manager, ECR and other services, names the other accounts or uses aws:PrincipalOrgID or aws:PrincipalOrgPaths. The caller keeps its own identity, and both its IAM policy and the resource policy must allow the action. Because the caller keeps its own permissions, it can, for example, read from a bucket in another account and write to one in its own in a single operation. The following table summarises the comparison.
sts:AssumeRoleaws:PrincipalOrgID / aws:PrincipalOrgPathsBoth sides must allow
Within one account, either an identity-based policy or a resource-based policy can grant access. Across accounts, both are required: the caller's own account must allow the action in an identity-based policy, and the target account must allow it in the resource policy or the role's trust policy. Organisation guardrails (SCPs on the caller, RCPs on the resource) apply on top of both, and an explicit deny anywhere wins.
The confused deputy problem
A vendor that assumes roles in many customers' accounts could be tricked by one customer into acting on another customer's account if role ARNs were the only check. The vendor therefore issues each customer a unique external ID, and each customer's role trust policy requires it with the sts:ExternalId condition. For AWS services acting on your behalf, the equivalent protection is the aws:SourceArn and aws:SourceAccount conditions in resource policies.
Encrypted resources across accounts
Encryption adds a third policy to every cross-account decision. A principal in another account can read an encrypted object, snapshot or message only if the KMS key policy allows that account or role, and the principal's own IAM policy allows it to use the key, in addition to the permissions on the resource itself. Forgetting the key policy is one of the most common causes of access denied errors in cross-account designs.
AWS managed keys, such as aws/s3 or aws/ebs, have key policies that cannot be edited, so they can never be used by another account. Any design that shares encrypted data across accounts, such as snapshots copied to a backup account, pipeline artifacts used by deployment accounts, or objects replicated to a log archive, therefore needs a customer managed key. Encrypted resources that are already protected with an AWS managed key must be re-encrypted with a customer managed key, for example by copying a snapshot, before they can be shared.
Auditing cross-account access
Cross-account access tends to accumulate: a bucket shared for a project stays shared after the project ends. IAM Access Analyzer, configured with the organisation as its zone of trust, continuously reports every resource and role that can be accessed from outside the organisation, such as buckets, KMS keys, roles and snapshots, so unintended external access is found and either archived as intended or removed.
For individual actions, CloudTrail records the AssumeRole call in both the calling and the target account, and the actions taken with the role in the target account. Readable trails depend on conventions: role session names that identify the caller, such as the pipeline execution ID or the user's name, and the source identity attribute, which is set when the first role is assumed and carried through any chain of further roles, so the original person can be identified even several hops later.
A vendor role and a cross-account data reader
Two integrations arrive together. First, a monitoring vendor needs read-only access to the company's accounts. The role it assumes requires the external ID the vendor issued uniquely to this customer, which prevents the confused deputy problem:
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::999988887777:role/VendorIntegration" },
"Action": "sts:AssumeRole",
"Condition": { "StringEquals": { "sts:ExternalId": "acme-7f3e9c21" } }
}Second, an analytics job in account 555555555555 must read files from a bucket in the data account and write results to its own bucket. Assuming a role in the data account would give up the job's permission to write locally, so a resource-based policy on the source bucket is the better fit; the job's own IAM policy must also allow s3:GetObject on the bucket:
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::555555555555:role/AnalyticsJob" },
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": ["arn:aws:s3:::acme-sales-data", "arn:aws:s3:::acme-sales-data/*"]
}If the bucket is encrypted with a KMS key, the key policy must also allow the analytics role to decrypt, which is only possible with a customer managed key. IAM Access Analyzer will report the vendor role as accessible from outside the organisation, which is expected and can be archived with a note.
A role in account B has s3:GetObject on a bucket in account A, but access fails. What else may be missing?
Show answer
Permission on account A's side: the bucket policy must allow the role, and if the objects use SSE-KMS, the key policy must allow it too. Cross-account access needs both sides to allow it.
Exam Tips
Remember for the exam
- Role assumption swaps the caller's permissions; resource-based policies let the caller keep its own.
- Across accounts, both the caller's identity policy and the resource or trust policy must allow the action.
- When a third party assumes a role, require an external ID.
- To share encrypted snapshots or backups, use a customer managed key with a cross-account key policy.
- To find resources shared outside the organisation, use IAM Access Analyzer with an organisation zone of trust.