Sub topic 13
Diagnosing role assumption failures
This chapter assumes you have already built the pieces from earlier chapters: an IAM OIDC provider for token.actions.githubusercontent.com, a role with an sts:AssumeRoleWithWebIdentity trust policy, and a workflow that uses aws-actions/configure-aws-credentials. Here you learn to find out why the exchange fails and how to fix it.
Where the exchange can break
A role assumption has four stages. Each stage has its own failure signature, so knowing the stage narrows the search quickly.
The fourth error is not a role assumption failure at all. The role was assumed, and the later service call was denied. Check aws sts get-caller-identity output in the job log to tell the two apart. If it prints the assumed-role ARN, the trust side works and you should look at the permissions policy.
Stage 1: the workflow cannot get a token
The action asks the runner for an OIDC token. The runner provides one only when the job has the id-token: write permission. Without it, the action fails before it contacts AWS, typically with a message that the OIDC token request variables are missing or that credentials could not be loaded.
Check these points:
permissionsmust includeid-token: write, at the workflow level or the job level.- When you set
permissionsat all, every permission you do not list becomesnone. Keepcontents: readif the job usesactions/checkout. - A job-level
permissionsblock replaces the workflow-level one. It does not merge with it. - Workflows triggered by pull requests from forks do not receive the OIDC token. This is a GitHub security rule, so deploy jobs should not rely on fork pull requests.
A correct block looks like this:
permissions:
id-token: write
contents: readStage 2: AWS does not recognize the provider
If the log says No OpenIDConnect provider found in your account for https://token.actions.githubusercontent.com, the provider is missing from the account that holds the role, or its URL is wrong. Check it:
aws iam list-open-id-connect-providers
aws iam get-open-id-connect-provider \
--open-id-connect-provider-arn arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.comConfirm three things. The account ID in the ARN is the account where the role lives. The URL is exactly https://token.actions.githubusercontent.com, with no trailing slash. The client ID list includes sts.amazonaws.com.
Stage 3: the trust policy rejects the token
The most common message is:
Not authorized to perform sts:AssumeRoleWithWebIdentityIt means AWS validated the token and then found that the role's trust policy did not allow it. The message does not say which condition failed, so compare the token's claims against the policy by hand.
Compare the actual `sub` with the policy
The sub claim depends on how the job was triggered. A mismatch here causes most failures.
| Trigger or setting | `sub` value |
|---|---|
| Push to `main` | `repo:my-org/my-repo:ref:refs/heads/main` |
| Pull request | `repo:my-org/my-repo:pull_request` |
| Job uses environment `production` | `repo:my-org/my-repo:environment:production` |
| Push of tag `v1.0` | `repo:my-org/my-repo:ref:refs/tags/v1.0` |
Two traps follow from the table. First, a job that declares environment: production gets an environment sub, even when it runs on main. A policy that only allows ref:refs/heads/main will reject it. Second, a pull request job gets the pull_request form, so a branch-based policy rejects it too.
To see the real value, print the claims from inside the job. The runner exposes a request URL and a bearer token as environment variables when id-token: write is set:
- name: Print OIDC claims
run: |
TOKEN=$(curl -sS -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" | jq -r .value)
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | jq '{iss, aud, sub}'The payload is the middle part of the JWT. The output shows only iss, aud, and sub. It is a token for debugging, so do not print the full token or share it. Remove this step after you finish.
Check the other conditions
- Audience. The
audclaim must equal the value in the:audcondition.configure-aws-credentialsrequestssts.amazonaws.comby default. If you changedaudiencein the action, change the policy to match. - Condition key prefix. The keys must start with
token.actions.githubusercontent.com:, matching the provider URL without the scheme. - Operator. A value with a wildcard, such as
repo:my-org/*, needsStringLike. UnderStringEqualsthe*is a literal character, and nothing matches. - Case and spelling. Organization and repository names in
subare compared as strings. Check for renamed or transferred repositories, because the old name stops matching. - Principal. The
FederatedARN must point to the provider in the same account as the role. - Action. The trust policy action must be
sts:AssumeRoleWithWebIdentity, notsts:AssumeRole.
Typical corrected policy
If you need both branch deployments and an environment, list the values in an array. StringLike accepts several values and matches any of them:
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": [
"repo:my-org/my-repo:ref:refs/heads/main",
"repo:my-org/my-repo:environment:production"
]
}
}Stage 4: the role works but the call is denied
After a successful assumption, errors like AccessDenied or is not authorized to perform: s3:PutObject come from the permissions policy. The error text names the action and the resource. Compare them with the policy:
- Bucket-level actions such as
s3:ListBucketapply to the bucket ARN. Object actions such ass3:PutObjectapply toarn:aws:s3:::bucket/*.aws s3 sync --deleteneedss3:ListBucket,s3:PutObject, ands3:DeleteObject. - A permissions boundary or a service control policy can deny an action that the role policy allows.
- The role session may be in the wrong region or account, so check
aws-regionand the ARN inrole-to-assume.
Other errors worth recognizing
| Symptom | Likely cause |
|---|---|
| `Could not assume role with OIDC: Request ARN is invalid` | The `role-to-assume` value is malformed or has a typo in the account ID. |
| `Incorrect token audience` | The token `aud` is not in the provider's client ID list. |
| Works on `main`, fails on a feature branch | The `sub` condition allows only `main`, which is correct. Fix the workflow trigger, not the policy. |
| Credentials expire during a long job | The default session lasts one hour. See the session duration chapter. |
| Works in one repository, fails in a copied one | The trust policy still names the old repository. |
A debugging routine
- Read the first error line in the job log. Decide which stage it belongs to.
- If it is before any AWS call, check
permissions. - If it names the provider, list the providers in the target account.
- If it says not authorized, print the claims and compare
subandaudwith the trust policy character by character. - If
get-caller-identitysucceeds, move to the permissions policy. - Use CloudTrail to confirm what AWS saw. A failed
AssumeRoleWithWebIdentityevent appears with an error code, and the request parameters show the role ARN that was requested.
Exercise
A deploy job declares environment: production and runs on a push to main. The role's trust policy has "token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:ref:refs/heads/main" under StringLike. The job fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". Why, and what are two ways to fix it?
Show answer
Because the job uses an environment, the sub claim is repo:my-org/my-repo:environment:production, not the branch form, so the condition does not match. Fix it either by changing the trust policy to allow the environment sub (or listing both values), or by removing environment: production from the job if you do not need protection rules. Allowing the environment form is the better choice, since it keeps the production approval gate in effect.
Exercise
The job log shows aws sts get-caller-identity printing an assumed-role ARN, and the next step fails with "not authorized to perform: s3:DeleteObject". Is this a trust policy problem? Where do you look?
Show answer
No. The role was assumed, so the trust policy and provider are working. Look at the role's permissions policy, and check that it allows s3:DeleteObject on arn:aws:s3:::my-example-bucket/*. Also check for a permissions boundary or service control policy that denies the action.