Sphere
Log in Join

GitHub Actions OIDC role assumption fails with 'could not assume role with OIDC'

I was migrating our deploy workflow off a static AWS access key that had been in GitHub Secrets since 2021. Standard OIDC setup: create an identity provider, create a role, use aws-actions/configure-aws-credentials@v4. It failed on the first run with an error that looked like an account-level problem.

## The error

Run aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy-prod
aws-region: us-east-1
Error: could not assume role with OIDC: An error occurred (InvalidIdentityToken) when calling the AssumeRoleWithWebIdentity operation: No OpenIDConnect provider found in your account for https://token.actions.githubusercontent.com

"No OpenIDConnect provider found in your account" sent me to IAM in the wrong account for about twenty minutes. We have three AWS accounts and the provider did exist in the deploy account.

## The trust policy was the problem

The identity provider was there. The role existed. What was wrong was the trust policy I had copied from a blog post:

{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:acme/infra:ref:refs/heads/main"
}
}
}

Two things were off. First, the audience in the GitHub token was sts.amazonaws.com, which is what configure-aws-credentials sends by default, so that part was fine. Second, the sub condition. Our workflow was a reusable workflow called from another repo, so the subject was repo:acme/infra/.github/workflows/deploy.yml@refs/heads/main, not the plain ref: form I had written. IAM reports the whole thing as "no provider found", which is a terrible error message for a condition mismatch.

## How I found the actual subject

Add a step that decodes the token before the credentials step:

- name: Show OIDC claims
run: |
curl -sS -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r '.value' | cut -d. -f2 | base64 -d | jq .

That printed the real sub and it was obvious. I changed the condition to:

"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:acme/infra/.github/workflows/deploy.yml@refs/heads/main"
}

For a normal workflow in the same repo, repo:acme/infra:ref:refs/heads/main is correct. For workflow_dispatch from a tag it is repo:acme/infra:ref:refs/tags/*. Be careful with :environment: subjects too; they need repo:org/repo:environment:prod.

I also learned that you can test the role from a terminal, which is much faster than pushing commits to see if a workflow works. Grab a token with the GitHub API using the same audience, then call sts assume-role-with-web-identity directly. If that fails, the problem is your trust policy and not the workflow syntax.

## The other thing that bit me

Even with the trust policy fixed, the token request itself failed until I added permissions at the job level:

jobs:
deploy:
runs-on: ubuntu-22.04
permissions:
id-token: write
contents: read

The repository default was read-only, so no token was issued. The error for that one was clearer:

Error: Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable

## Was it worth it

Yes, mostly. No more key rotation, CloudTrail shows which workflow assumed the role, and the role is scoped to one repo. It took the better part of an afternoon because AWS hides condition failures behind a provider error. Decode the token first and you will save yourself that afternoon.