Lesson 2 of 6 Identity Federation and Cross-Account Access

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.

Role-based
HowA role in the target account trusts the source account; source principals may sts:AssumeRole
Caller's identityTemporarily gives up its own permissions
Typical useAdministration and deployment into another account
Third partiesRequire an external ID to prevent the confused deputy problem
Resource-based
HowPolicies on S3, KMS, SQS, SNS, Lambda, Secrets Manager, ECR and more name other accounts, or use aws:PrincipalOrgID / aws:PrincipalOrgPaths
Caller's identityKeeps its own identity; both its IAM policy and the resource policy must allow
Typical useReading data from another account while writing to one's own
Third partiesRarely appropriate

The following diagram contrasts the two approaches.

Two ways to grant cross-account access. With a role, the caller in account A assumes a role in account B that trusts account A, and acts with only the role's permissions. With a resource-based policy, the caller keeps its own identity, so it can read a bucket in account B whose policy names it and write to its own bucket in account A in one operation; its IAM policy and the bucket policy must both allow the read.
The resource-based approach is preferred when the caller must also work with resources in its own account in the same operation.

Both 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.

The following diagram shows how the external ID defeats the confused deputy problem.

The confused deputy problem and the external ID. Customer B enters customer A's role ARN in a vendor's settings. The vendor always assumes roles with the external ID it issued to the customer whose settings it is using, so its request to customer A's role carries B's ID and is denied, while its request to B's own role succeeds.
The external ID works because the customer cannot choose it: the vendor issues it and always sends the ID that belongs to the customer whose settings it is using.
Scenario questions

Preventing confused deputies

Exam skills 2.1.3

Protect roles and resources from the confused deputy problem.

Log in to try this activity Interactive activities are free with an account.

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.

The following diagram shows the three policies that must all allow a cross-account read of an encrypted object.

Reading an encrypted object across accounts takes three allows. The caller's IAM policy in account A must allow s3:GetObject and kms:Decrypt, the bucket policy in account B must allow account A, and the key policy of the customer managed key in account B must allow account A to decrypt. AWS managed keys such as aws/s3 cannot be shared, because their key policies cannot be edited.
SCPs on the caller and RCPs on the resources still apply on top of these three, and an explicit deny anywhere wins.
Troubleshooting scenario

Encrypted cross-account access

Exam skills 2.1.3 · 2.2.1

Fix cross-account access to encrypted resources.

Log in to try this activity Interactive activities are free with an account.

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.

Worked example

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.

Check yourself

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.