Sub topic 6
Requesting a token and assuming the role from a workflow
This chapter assumes you know GitHub Actions workflow YAML, what an AWS IAM role is, and what a JWT is. Earlier chapters covered the claims in the token and how a trust policy matches them. Here you connect the two from the workflow side.
What the workflow has to do
A trust policy only matters if the workflow can get a token to present. Two steps make that happen:
- The job asks GitHub for an OIDC token. This needs the
id-token: writepermission. - The job sends the token to AWS Security Token Service (STS) with
sts:AssumeRoleWithWebIdentity. AWS checks it against the role's trust policy and returns short-lived credentials.
The aws-actions/configure-aws-credentials action does both steps for you. It then exports the credentials as environment variables for later steps.
A complete workflow
This workflow matches the environment-restricted trust policy from the earlier chapter. Its job uses environment: production, so the sub claim ends in environment:production.
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-identityThe last step prints the account and the assumed-role ARN. If it succeeds, the trust policy accepted the token.
Why the permissions block matters
The permissions block sets what the job's GITHUB_TOKEN and OIDC access can do. Two points matter here:
id-token: writelets the job request an OIDC token. It does not give write access to your repository or to AWS.- When you set any
permissionskey, every permission you leave out becomesnone. That is why the example addscontents: read. Without it,actions/checkoutfails on a private repository.
You can set permissions at the workflow level or the job level. Prefer the job level, so that only the jobs that need to assume a role can request a token.
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- run: echo "Only this job can request an OIDC token"The audience the action sends
The action requests a token with the audience sts.amazonaws.com by default. This matches the aud condition in the trust policy. If you set a different audience through the action's audience input, you must change the trust policy to match. Otherwise AWS rejects the token.
Common errors
| Error message | Likely cause |
|---|---|
| `Credential could not be loaded` or a missing token request URL | The job lacks `id-token: write`, or the workflow runs from a forked pull request where the token is not issued. |
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | A trust policy condition did not match. Check `sub` and `aud` first. |
| `No OpenID Connect provider found` | The IAM OIDC provider for `token.actions.githubusercontent.com` is not created in the account. |
When the error is the second one, compare the sub your job produces with the string in the trust policy, character by character. A job that sets environment: produces an environment: subject. It does not produce a ref: subject, even when it runs on main.
Exercise
A deploy job has environment: production and runs on a push to main. Its role trust policy requires the sub value repo:example-org/example-repo:ref:refs/heads/main. The job fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". Why, and what are two ways to fix it?
Show answer
When a job declares an environment, the sub claim becomes repo:example-org/example-repo:environment:production, not the ref: form. The trust policy does not match. Fix it either by changing the trust policy to require the environment:production subject, or by removing environment: from the job so the subject uses the branch form. The first fix is preferred for production, because it lets environment protection rules gate the role.