Sub topic 15
Troubleshooting AccessDenied and auditing with CloudTrail
This chapter assumes you know IAM users, roles, and JSON policies, and that you have already written a trust policy and a caller policy for a cross-account role. It shows how to find out why an AssumeRole call or a call made with the assumed role fails, and how to confirm afterwards who did what.
Where a cross-account request can fail
A cross-account AssumeRole call is checked in more than one place. Each check can deny the request independently, so the first step is to work out which check failed.
- The caller's own account must allow
sts:AssumeRoleon the role ARN (the caller policy). - The role's trust policy must name the caller and satisfy every condition.
- Guardrails in either account (service control policies, permissions boundaries, session policies) must not block the action.
- After the role is assumed, the role's permissions policy must allow the actual API call.
Failures in steps 1 to 3 happen at AssumeRole. Failures in step 4 happen later, on a call such as s3:ListBucket. Knowing which phase you are in cuts the search in half.
Read the error message first
Current AWS services often include the reason in the message. Compare these two messages.
An error occurred (AccessDenied) when calling the AssumeRole operation:
User: arn:aws:iam::111111111111:user/alice is not authorized to perform:
sts:AssumeRole on resource: arn:aws:iam::222222222222:role/ReadOnlyAuditorThis message names the caller and the role, and the wording "is not authorized to perform" points at the caller's identity policy, or at the trust policy if the caller's policy is already correct. Some messages end with "because no identity-based policy allows the sts:AssumeRole action". That phrase means the caller policy is missing. Other messages say "with an explicit deny in a service control policy", which sends you straight to AWS Organizations.
An error occurred (AccessDenied) when calling the ListBuckets operation:
User: arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-session
is not authorized to perform: s3:ListAllMyBuckets
because no identity-based policy allows the s3:ListAllMyBuckets actionHere the principal is an assumed-role ARN in account 222222222222. AssumeRole worked, and the role's permissions policy is what lacks the action.
Start by confirming who you are
Many failures come from running the command as a different identity than you think. Check before you debug policies.
aws sts get-caller-identity
aws sts get-caller-identity --profile auditorThe first command shows the identity the CLI picks by default, including any AWS_ACCESS_KEY_ID environment variables that override your profile. The second shows what the auditor profile resolves to. If the account ID in the output is the role's account and the ARN contains assumed-role, the role was assumed. If it still shows account 111111111111, the profile is not being used.
Common causes and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| `AssumeRole` denied, no identity-based policy allows it | The caller has no `sts:AssumeRole` permission for this role ARN | Add an identity policy in the trusted account with the exact role ARN as `Resource` |
| `AssumeRole` denied, caller policy is correct | Trust policy does not list the caller's account or principal | Update the trust policy `Principal` in the trusting account |
| `AssumeRole` denied only for some callers | Trust policy names specific users or roles, and the caller is not one of them | Add the principal, or trust the account root and control access with caller policies |
| `AssumeRole` denied only through a third-party tool | `sts:ExternalId` condition does not match what the tool sends | Compare the value in the trust policy with the value configured in the tool |
| `AssumeRole` denied with MFA condition | Session was not authenticated with MFA | Obtain a session with MFA and pass `--serial-number` and `--token-code` |
| Denied with "explicit deny in a service control policy" | An SCP in the organization blocks the action | Review SCPs on the caller's and the role's accounts and organizational units |
| `AssumeRole` works, later calls are denied | Role permissions policy lacks the action or resource | Add the missing action to the role permissions policy |
| Denied after role chaining | Chained session duration or session policy limits | Check the one-hour chaining limit and any session policy |
| Principal in the trust policy shows as an unique ID | The named user or role was deleted and recreated | Update the trust policy with the new principal |
Typical mistakes in the policies
The most frequent mistake is writing only one side of the trust. Cross-account access needs both sides, so a correct trust policy with no caller policy still fails. When the caller is an IAM user or role, the caller policy must name the target role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::222222222222:role/ReadOnlyAuditor"
}
]
}A second mistake is a typo in the role ARN or account ID. The policy is valid JSON and validates without errors, but it matches nothing. Copy the ARN from the IAM console or from aws iam get-role instead of typing it.
A third mistake involves the trust policy principal. When the principal is the account root, as in arn:aws:iam::111111111111:root, the trust policy delegates the decision to account 111111111111, and that account must grant the permission to each caller. When the principal is a specific user or role, that principal is trusted directly and the same-account rules differ, but for a cross-account call the caller still needs its own sts:AssumeRole permission. Do not assume that naming a user in the trust policy replaces the caller policy.
A fourth mistake is a trust policy condition that the request cannot satisfy. For example, a condition on aws:PrincipalOrgID fails if the caller's account is not in that organization, and a condition on sts:ExternalId fails if the caller passes no external ID.
Effects of session policies and boundaries
An explicit Deny in any applicable policy wins over every Allow. If the trusted and trusting accounts both look correct, search for a Deny statement in SCPs, permissions boundaries on the caller or the role, and any session policy passed at AssumeRole time. A permissions boundary on the role limits what the role can do even if its permissions policy allows more. A session policy limits the session to the intersection of the role's permissions and the session policy.
Test the role with the policy simulator and the CLI
You can check a role's permissions without making the real call. The IAM policy simulator evaluates the identity policies, boundaries, and (when you supply them) SCPs for a principal.
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::222222222222:role/ReadOnlyAuditor \
--action-names s3:ListBucket \
--resource-arns arn:aws:s3:::example-audit-bucketRun this in account 222222222222. The output lists EvalDecision as allowed, implicitDeny, or explicitDeny. An implicitDeny means no policy allows the action. An explicitDeny names the statement that blocks it. The simulator does not evaluate resource policies on the target bucket unless you pass them, so a simulated allow does not guarantee that the real call works.
Audit with CloudTrail
CloudTrail records AssumeRole calls and the API calls that follow. Use it to answer two questions: why a request was denied, and who used a role.
Where the events are logged
Two accounts are involved, and each one logs its own side.
- The trusted account (111111111111) logs the caller's attempt to call
sts:AssumeRole. - The trusting account (222222222222) logs the same
AssumeRoleevent and every API call the session makes afterwards.
STS is a global service with regional endpoints, so check the Region you actually called. Management events such as AssumeRole appear in the CloudTrail event history for 90 days at no cost. For longer retention, create a trail that delivers events to Amazon S3.
Find the AssumeRole events
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=AssumeRole \
--max-results 5 \
--region us-east-1A denied attempt carries an errorCode and an errorMessage in the event record. A successful event includes the fields that matter for auditing:
userIdentityis the caller, with its ARN and account ID.requestParameters.roleArnandrequestParameters.roleSessionNameshow the target and the session name.requestParameters.externalIdis not shown in plain text for some services, so do not depend on it in searches.responseElements.credentials.accessKeyIdis the key ID of the temporary credentials.sourceIPAddressanduserAgentshow where the call came from.
Follow a session through the trail
The session name you pass to AssumeRole appears in the ARN of every later call: arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-session. That makes the name your audit handle. Require meaningful names, such as the user's name or a ticket number, so that events can be tied back to a person. A trust policy can enforce this with the sts:RoleSessionName condition key. To link a later call to its original caller, use the accessKeyId from the AssumeRole response and match it against userIdentity.accessKeyId in the later events.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=AccessKeyId,AttributeValue=ASIAEXAMPLEKEYID \
--region us-east-1Replace the key ID with the one from the AssumeRole response. Events can take several minutes to appear in CloudTrail, so a missing event right after a call does not mean it was not logged.
Set a source identity
With --source-identity, the caller supplies a value that stays with the session and appears in CloudTrail for every action, even across role chaining. The role's trust policy must allow sts:SetSourceIdentity. This helps when many people share one role, because the session name can be changed at each hop but the source identity cannot.
A working method
- Run
aws sts get-caller-identityand confirm the identity and account. - Read the full error message and note whether the principal is a user or an
assumed-roleARN. - If
AssumeRolefailed, check the caller policy, then the trust policy, then conditions, then SCPs and boundaries. - If a later call failed, check the role's permissions policy, the resource policy, and any session policy.
- Look up the event in CloudTrail in the account where the failure happened and read
errorMessage. - Change one thing at a time and retest, so you know which change fixed it.
Exercise
A user in account 111111111111 runs aws sts assume-role for arn:aws:iam::222222222222:role/ReadOnlyAuditor and gets AccessDenied. The trust policy in account 222222222222 lists arn:aws:iam::111111111111:root as principal with no conditions. aws sts get-caller-identity shows the user is correct. Name the most likely missing piece and where you would add it, and name the CloudTrail field that confirms the failure reason.
Show answer
The user's identity policy (or the policy of a group or role they use) in account 111111111111 most likely lacks sts:AssumeRole on arn:aws:iam::222222222222:role/ReadOnlyAuditor. Add that Allow statement in the trusted account. Confirm it with the errorMessage field of the failed AssumeRole event in CloudTrail, which states that no identity-based policy allows the action. An SCP or permissions boundary on the user is the next place to look if the policy already exists.
Exercise
After a successful AssumeRole, aws s3 ls s3://example-audit-bucket --profile auditor fails with AccessDenied, and the error shows the principal arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/audit-session. Which policy should you check first, and why is the trust policy not the problem?
Show answer
Check the role's permissions policy in account 222222222222 first, then any permissions boundary, session policy, SCP, and the bucket policy. The trust policy only controls who can assume the role. Because the principal is an assumed-role ARN, the assumption already succeeded, so the trust policy and caller policy are not the cause.