`Error: Cannot assume IAM Role` — `api error AccessDenied: User: arn:aws:iam::111111111111:user/ci is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::222222222222:role/terraform`.
IntermediateTroubleshootingAuthentication and Cloud API Errors
Answer
Likely causes → The target role's trust policy doesn't trust the caller, the caller's identity policy lacks sts:AssumeRole on that ARN, a required ExternalId is missing, or an SCP denies it.
Diagnose →
aws iam get-role --role-name terraform --query 'Role.AssumeRolePolicyDocument' # run in account 222
aws sts assume-role --role-arn arn:aws:iam::222222222222:role/terraform --role-session-name test
Fix → Add the caller as Principal in the trust policy and sts:AssumeRole in the caller's policy; pass external_id in assume_role {} if the trust policy requires it.
Prevent → Manage trust relationships in Terraform in a central IAM stack, tested by a pipeline smoke test.
More on Authentication and Cloud API Errors
`Error: No valid credential sources found` followed by `no EC2 IMDS role found ... context deadline exceeded`.BeginnerGitHub Actions: `Could not assume role with OIDC: Not authorized to perform sts:AssumeRoleWithWebIdentity`.IntermediateGitHub Actions log says `It looks like you might be trying to authenticate with OIDC. Did you mean to set the id-token permission?`BeginnerApplies that run longer than an hour fail with `ExpiredToken: The security token included in the request is expired`.Intermediate`InvalidClientTokenId: The security token included in the request is invalid.`Beginner`failed to refresh cached credentials, refresh cached SSO token failed, unable to refresh SSO token ... InvalidGrantException`.Beginner