Sub topic 13
Diagnosing role assumption failures
This chapter assumes you have already created the IAM OIDC provider, a role with a trust policy, and a workflow that uses aws-actions/configure-aws-credentials. It covers what goes wrong when those pieces do not line up, and how to find the cause quickly.
Where the failure happens
Role assumption has three hops. A failure at each hop produces a different message, so read the error before changing anything.
Common errors
| Message in the job log | Likely cause | Fix |
|---|---|---|
| `Credentials could not be loaded` or no OIDC token URL | The job lacks `permissions: id-token: write` | Add the permission at workflow or job level |
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | The `sub` or `aud` condition does not match the token | Compare the token claims with the trust policy |
| `No OpenIDConnect provider found in your account` | The provider is missing or in another account | Create it in the account that owns the role |
| `Could not assume role` with a wrong ARN | Typo in `role-to-assume` or wrong account ID | Copy the ARN from the IAM console |
The id-token permission
If you set a permissions block, every permission you leave out becomes none. Include contents: read next to id-token: write, or actions/checkout fails on private repositories.
permissions:
id-token: write
contents: readSubject mismatches
The most frequent cause of "not authorized" is a sub value that differs from what you expected. The sub claim changes with the trigger:
- A push to a branch gives
repo:my-org/my-repo:ref:refs/heads/main. - A pull request gives
repo:my-org/my-repo:pull_request. - A job that declares
environment: productiongivesrepo:my-org/my-repo:environment:production, even on the main branch.
A trust policy that expects the branch form will reject a job that uses an environment. Also check capitalization and the organization name, because StringLike and StringEquals are case sensitive.
A debugging routine
- Read the exact error text in the failing step.
- Confirm the workflow has
id-token: writeand that the job is not running from a forked pull request, which gets no token. - Check the
role-to-assumeARN and the account ID. - Compare the
audin the trust policy with the token audience. The default issts.amazonaws.com. - Compare the
subin the trust policy with the value your trigger produces. - Check CloudTrail for the
AssumeRoleWithWebIdentityevent. Its error message often names the condition that failed.
Exercise
A deploy job declares environment: production and runs on main. The role trust policy requires repo:my-org/my-repo:ref:refs/heads/main and returns "not authorized". Why, and what is the fix?
Show answer
When a job uses an environment, the sub claim becomes repo:my-org/my-repo:environment:production, not the branch form. Change the trust policy sub condition to the environment value. Alternatively, list both values, or remove environment from the job if you do not need protection rules.