Sub topic 12
Troubleshooting common failures
This chapter assumes you have read the earlier chapters. You should know the id-token: write permission, the aud and sub claims, and how an AWS IAM trust policy uses conditions. Most OIDC failures come from three places: the workflow cannot get a token, the token has the wrong audience, or the token has a different subject than the trust policy expects.
Where a failure can happen
The exchange has several steps, and each one fails in its own way. Finding the failing step is most of the work.
Failure 1: missing permission
If the job has no id-token: write permission, GitHub never gives the runner the request variables. The credentials action fails with a message that it could not get ACTIONS_ID_TOKEN_REQUEST_URL.
Check the permissions block at both the workflow level and the job level. A job-level permissions block replaces the workflow-level one. If you set permissions anywhere, any permission you leave out becomes none, so keep contents: read if the job also checks out code.
Pull requests from forks get a read-only token. They cannot request an OIDC token, so a deploy job triggered that way will fail by design.
Failure 2: audience mismatch
The aud claim in the token must equal the value your trust policy checks. For AWS this is normally sts.amazonaws.com. The configure-aws-credentials action requests that audience by default. A mismatch usually appears when you request a token by hand with a different audience value, or when you changed the audience in the provider setup.
Also confirm that the IAM OIDC provider exists in the same AWS account as the role, with the URL https://token.actions.githubusercontent.com.
Failure 3: subject mismatch
This is the most common failure. AWS returns Not authorized to perform sts:AssumeRoleWithWebIdentity. It does not say which condition failed, so you must compare the token's claims with your policy yourself.
Typical causes:
- The trust policy names
refs/heads/main, but the job ran on another branch or on a tag. - The job uses a GitHub environment. Its
subis thenrepo:octo-org/octo-repo:environment:production, not a ref. - The job was triggered by
pull_request. Itssubisrepo:octo-org/octo-repo:pull_request. - The repository was renamed or moved, or the case of the owner or repository name differs.
- A custom subject claim template changed the format.
Inspecting the real claims
Do not guess. Print the claims of the token the job actually received. The payload of a JWT is base64url-encoded JSON, so you can decode it in a step. The token expires quickly and the payload holds no secret key material, but treat the full token as sensitive and print only the decoded payload.
name: debug-oidc
on: workflow_dispatch
permissions:
id-token: write
contents: read
jobs:
show-claims:
runs-on: ubuntu-latest
steps:
- name: Print token 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 '{aud, sub, iss, ref, event_name}'Compare the printed sub with the StringEquals value in the trust policy, character by character. If you use wildcards, switch the condition operator to StringLike, because StringEquals does not treat * as a wildcard.
Other checks
- Role ARN typos: the
role-to-assumevalue must match the role in the account you expect. - Region: set
aws-regionso that the STS call goes to a valid endpoint. - Time: the token lifetime is short, so a job that delays between requesting and using the token can see it expire. Request the token close to where you use it.
- Trust policy changes can take a short time to apply. Retry once before changing anything else.
Exercise
A deploy job runs in a GitHub environment named production. The trust policy only allows repo:octo-org/octo-repo:ref:refs/heads/main. AWS returns "Not authorized to perform sts:AssumeRoleWithWebIdentity". What is wrong, and how do you fix it?
Show answer
When a job uses an environment, the sub claim becomes repo:octo-org/octo-repo:environment:production, so the ref-based condition does not match. Change the trust policy's sub condition to that environment value (or add it as an additional allowed value). Decode the token's claims to confirm the exact string.