Sub topic 5
Writing a Role Trust Policy
A trust policy is the part of an AWS IAM role that says who may assume it. For GitHub Actions, it decides which workflow runs can exchange an OIDC token for temporary credentials. This chapter assumes you know what a JWT is, can read workflow YAML, and have a basic grasp of IAM roles. It also assumes the GitHub OIDC provider is already registered in your AWS account.
The three parts of the policy
A trust policy for GitHub OIDC has three parts that matter:
- Principal: the OIDC provider, written as a
FederatedARN. It names who vouches for the token. - Action:
sts:AssumeRoleWithWebIdentity. This is the call that trades a token for credentials. - Condition: tests on the token's claims. Without conditions, any token signed by GitHub could assume the role, including tokens from other people's repositories.
The conditions are where the security lives. Two claims matter most: aud (the audience) and sub (the subject). AWS exposes them as condition keys prefixed with the provider's host name, token.actions.githubusercontent.com.
A policy for one repository and branch
This policy allows only workflows on the main branch of example-org/example-repo.
{
"Version": "2012-10-17",
"Statement": [
{
"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",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}
]
}Both conditions sit under one StringEquals operator, so both must be true. The audience check confirms the token was minted for AWS STS. The subject check confirms the run came from that repository and branch.
How a request is evaluated
Every check must pass. A failure at any step returns an AccessDenied error, and the error message does not tell you which condition failed. When debugging, compare the token's actual sub value to your condition string character by character.
The workflow side
The trust policy only works if the workflow can request a token. That needs the id-token: write permission. Without it, the job cannot get an OIDC token and the credentials step fails.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
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 assumed role's identity, which confirms the exchange worked.
Common errors
| Symptom | Likely cause |
|---|---|
| `Could not assume role with OIDC` | The `sub` or `aud` condition does not match the token |
| Token request fails in the job | `id-token: write` is missing |
| Works on `main`, fails on a pull request | The `sub` for pull requests has a different format |
| Role is assumable from other repositories | The `sub` condition is missing or too broad |
Exercise
A trust policy has the correct Principal and Action, and a StringEquals condition on aud only. Why is this unsafe, and which condition fixes it?
Show answer
Every GitHub Actions token has the audience sts.amazonaws.com, so any repository could assume the role. Adding a condition on token.actions.githubusercontent.com:sub that names your repository and branch, for example repo:example-org/example-repo:ref:refs/heads/main, restricts it to the intended workflows.
Exercise
Your deploy job fails with a message that it could not request an OIDC token. The trust policy is correct. What do you check in the workflow?
Show answer
Check that the workflow or job has permissions with id-token: write. Without it, GitHub does not issue the token.