Sub topic 6
Requesting a token and assuming the role from a workflow
This chapter covers the workflow side of OIDC trust. A trust policy only helps if the workflow can request a token and exchange it for credentials. Most failures at this step come from a missing permission or a mismatch between what the workflow sends and what the policy expects.
It assumes you have the prerequisites from the plan: GitHub Actions workflow YAML, AWS IAM roles, and the idea of a JWT. It also assumes an OIDC provider for token.actions.githubusercontent.com already exists in your AWS account, and that a role with a trust policy like the ones in earlier chapters exists.
What happens during a run
When a job runs, GitHub does not hand it a token automatically. The job has to ask for one, and GitHub only agrees if the workflow has been granted permission. The exchange has four steps:
- The workflow declares
id-token: write. This makes GitHub inject two environment variables into the job,ACTIONS_ID_TOKEN_REQUEST_URLandACTIONS_ID_TOKEN_REQUEST_TOKEN. - A step calls the request URL, authenticating with the request token and naming an audience. GitHub returns a signed JWT whose claims describe this specific run.
- The step sends the JWT to AWS STS with
AssumeRoleWithWebIdentity. STS verifies the signature against the provider, then evaluates the role's trust policy against the claims. - If the conditions match, STS returns temporary credentials. They expire, 1 hour by default with the action below.
The permissions block
The first requirement is the permissions key. Without id-token: write, the two environment variables are never set and the job cannot request a token.
permissions:
id-token: write
contents: readid-token: write does not give the workflow write access to your repository. It only allows the job to request an OIDC token for itself. The word "write" is a quirk of how the permission is named.
Two details cause confusion:
- When you add a
permissionsblock, every permission you do not list is set tonone. That is whycontents: readappears above. Without it,actions/checkoutfails on a private repository. - You can set permissions at the workflow level or on a single job. Setting
id-token: writeon only the job that needs it follows least privilege, because other jobs in the workflow cannot mint tokens.
The standard approach: configure-aws-credentials
The workflow from the plan uses the official AWS action to do steps 2 to 4 for you:
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/example-deploy-role
aws-region: us-east-1
- run: aws sts get-caller-identityEach part does the following:
environment: productionmakes the token'ssubclaim end inenvironment:production. This is the value the environment-based trust policy matches. If you remove this line, the subject falls back to theref:form and an environment-only policy rejects the job.role-to-assumeis the full ARN of the role. A typo in the account ID or role name produces an authorization failure that looks like a trust problem.aws-regionis required because STS and later API calls need a region.- The action's
audienceinput defaults tosts.amazonaws.com. That default matches theaudcondition in the trust policies in this tutorial. If you change the input, change the policy to match. - The final step,
aws sts get-caller-identity, confirms that the credentials work. The output shows an assumed-role ARN, not an IAM user.
After the action runs, it exports AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN for the remaining steps in that job. Steps before it, and other jobs, do not get them. Each job that needs AWS access must run the action itself.
The role's permissions policy is a separate document from the trust policy. The trust policy decides who may assume the role. The permissions policy decides what the role can do once assumed. Passing the trust check with an empty permissions policy gives you credentials that can do nothing.
Seeing the claims your job actually sends
When a trust policy rejects a job, guessing at the sub value wastes time. It is faster to print the claims the job really produces. This workflow requests a token manually and prints only selected claims.
name: inspect-claims
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
environment: production
steps:
- name: Print selected 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 "::add-mask::$TOKEN"
export TOKEN
python3 - <<'PY'
import base64, json, os
payload = os.environ["TOKEN"].split(".")[1]
payload += "=" * (-len(payload) % 4)
claims = json.loads(base64.urlsafe_b64decode(payload))
for key in ("aud", "sub", "ref", "environment", "job_workflow_ref"):
print(key, "=", claims.get(key))
PYThe ::add-mask:: line stops the raw token from appearing in logs if a later command prints it. Print only the claims, never the token. A token is short-lived, but it is still a credential for the duration of its life.
Compare the printed sub character by character with the sub condition in your trust policy. Common differences are the wrong repository name, ref:refs/heads/main where the job ran with an environment, and different capitalization.
Troubleshooting failures
The error text identifies which stage failed.
| Symptom | Stage that failed | Usual cause |
|---|---|---|
| Error about `ACTIONS_ID_TOKEN_REQUEST_URL` not being set | Token request | `id-token: write` is missing, or a job-level `permissions` block overrides the workflow-level one |
| "Not authorized to perform sts:AssumeRoleWithWebIdentity" | Trust policy evaluation | The `sub` or `aud` condition does not match the token, or the role ARN is wrong |
| "No OpenIDConnect provider found" | Signature verification | The OIDC provider for `token.actions.githubusercontent.com` is not registered in that AWS account |
| Role assumed, but API calls return access denied | Permissions policy | The role's permissions policy lacks the needed actions |
Exact wording varies between action versions, so match on the stage rather than the text.
Runs that cannot get a token
Some runs cannot get a usable token, and that is often the intended behavior.
- Pull requests from forks do not receive
id-token: write, even if the workflow requests it. This protects your role from code written by outsiders. A deploy workflow should not run onpull_requestfrom forks anyway. - A pull request from a branch in the same repository can get a token. Its
subhas the formrepo:ORG/REPO:pull_request, which your branch-only and environment-only policies do not match. That rejection is useful. The next chapters cover when to allow it deliberately. - A job that has no
environmentkey gets a subject without an environment. A policy that requires one rejects it.
A note on the audience
The aud claim records who the token is for. AWS expects sts.amazonaws.com when the provider is set up the usual way. The workflow requests that audience, and the trust policy's StringEquals on aud pins it. If the workflow asked for a different audience, the token would still be valid and signed, but the condition would fail. The check ensures a token issued for another service cannot be replayed against your role.
Exercise
A deploy job has permissions: { contents: read } at the job level and id-token: write at the workflow level. The configure-aws-credentials step fails because the token request URL is not set. Why, and what is the smallest fix?
Show answer
The job-level permissions block replaces the workflow-level one instead of merging with it, so the job has no id-token: write. Add id-token: write to the job's own permissions block, next to contents: read.
Exercise
The role assumption fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". The trust policy requires sub to equal repo:example-org/example-repo:environment:production, and the job has no environment key and runs on a push to main. What sub does the job send, and how do you fix it?
Show answer
The job sends repo:example-org/example-repo:ref:refs/heads/main, because without an environment the subject uses the branch form. Add environment: production to the job so the subject ends in environment:production. Alternatively, change the policy to the branch form, but only if that matches your intent.