Sub topic 10
Putting it together: assuming an AWS role
This chapter assumes you know basic GitHub Actions syntax, what an IAM role is, and how a JWT carries claims. You will connect a workflow to an AWS IAM role with no stored access keys.
The pieces you need
Three things must exist before the workflow can run:
- An IAM OIDC identity provider in your AWS account for
token.actions.githubusercontent.com. - An IAM role whose trust policy accepts tokens from that provider.
- A workflow that requests a token and exchanges it for temporary credentials.
The role also needs a permissions policy for whatever the job does, such as writing to S3. The trust policy only controls who can assume the role. It grants no actions.
The exchange step by step
You do not write these calls yourself. The aws-actions/configure-aws-credentials action requests the token, calls STS, and exports the temporary credentials as environment variables for later steps.
The trust policy
This trust policy lets one repository's main branch assume the role:
{
"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:octo-org/octo-repo:ref:refs/heads/main"
}
}
}
]
}The Principal names the OIDC provider. The aud condition checks that the token was minted for AWS. The sub condition checks who is asking. Without the sub condition, any repository on GitHub could present a valid token to your role.
The workflow
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/github-deploy
aws-region: us-east-1
- run: aws sts get-caller-identityid-token: write lets the job request a token. Setting permissions explicitly removes the defaults for other scopes, so contents: read is listed to keep actions/checkout working.
Verify the result
The last step prints the identity AWS sees. The output looks like this, with a session name chosen by the action:
{
"UserId": "AROAEXAMPLEID:GitHubActions",
"Account": "123456789012",
"Arn": "arn:aws:sts::123456789012:assumed-role/github-deploy/GitHubActions"
}An assumed-role ARN confirms that the job holds temporary credentials for github-deploy. They expire, by default after one hour, so nothing long-lived is left behind to leak.
Common first-run failures
| Symptom | Likely cause |
|---|---|
| Action reports it could not get an ID token | `id-token: write` is missing from the workflow or job |
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | The `sub` or `aud` in the trust policy does not match the token |
| Provider not found | The IAM OIDC provider was not created in this account |
Exercise
Your deploy job runs from a pull request and fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". The trust policy is the one shown in this chapter. What is the most likely cause, and what would you change?
Show answer
The token's sub claim for a pull request run (repo:octo-org/octo-repo:pull_request) does not equal the policy's value, repo:octo-org/octo-repo:ref:refs/heads/main, so the StringEquals condition fails. If pull request runs should have access, add that exact sub value to the condition as a list entry. Consider granting a separate role with fewer permissions for those runs.