Sub topic 9
Allowing the workflow to request a token
This chapter assumes you can read a GitHub Actions workflow file, know what an IAM role and trust policy are, and have already registered the GitHub OIDC provider in IAM. Here you will give a workflow permission to ask GitHub for an OIDC token. Without that permission, the token never exists and AWS has nothing to verify.
Why the token is opt-in
GitHub does not create an OIDC token for every job. A job receives one only when its GITHUB_TOKEN permissions include id-token: write. This default protects you. A workflow that never needs AWS access cannot mint an identity token, and a pull request from a fork cannot obtain one with write access.
The word write can mislead you. This permission does not let the job write to your repository. It only lets the job request a signed token from GitHub's token service.
Setting the permissions
Set permissions at the workflow level or at the job level. Job-level settings override workflow-level ones for that job.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4You need both lines:
id-token: writelets the job request the OIDC token.contents: readletsactions/checkoutread your code.
Once you declare any permissions key, every permission you leave out is set to none. If you list only id-token: write, checkout fails on a private repository because contents is no longer readable.
Scoping the permission to one job
If a workflow has several jobs, grant the token permission only to the job that talks to AWS.
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "No AWS access needed here"
deploy:
needs: test
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4The test job cannot request a token. Any third-party action it runs cannot request one either. This keeps your trust surface small.
What GitHub adds to the job
When the permission is present, the runner receives two environment variables: ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN. An action calls that URL with the request token and gets back a signed JWT. In the next chapter, aws-actions/configure-aws-credentials does this for you, so you rarely call the URL by hand.
A common error
If you leave out the permission, the credentials step fails with a message like this:
Error: Could not get ID token. Did you grant id-token: write permission to this job?The fix is to add id-token: write at the workflow level or on the failing job. Also check that a job-level permissions block does not replace a workflow-level one that held the setting.
Exercise
A deploy job has permissions: { id-token: write } and nothing else. The AWS credentials step works, but actions/checkout fails on a private repository. Why, and how do you fix it?
Show answer
Declaring any permissions sets all unlisted permissions to none, so contents is no longer readable. Add contents: read to the same permissions block.