Sub topic 10
Putting it together: assuming an AWS role
This chapter assumes you know basic workflow syntax, IAM roles and policies, and the token claims from the earlier chapters. You will wire up the pieces on the AWS side and the GitHub side, then check that the role was assumed.
The moving parts
Four things must agree before a job can assume a role:
- An IAM OIDC identity provider in your AWS account. It tells AWS to accept tokens issued by
https://token.actions.githubusercontent.com. - An IAM role whose trust policy names that provider and restricts the audience and subject.
- A permissions policy on the role that defines what the job may do once it has the role.
- A workflow with
id-token: writethat requests a token and exchanges it for credentials.
The trust policy decides who can become the role. The permissions policy decides what the role can do. Keep these two ideas separate when you debug.
Step 1: Create the identity provider
You create the provider once per AWS account. Every role in the account can then reuse it.
aws iam create-open-id-connect-provider \
--url https://token.actions.githubusercontent.com \
--client-id-list sts.amazonaws.comThe --client-id-list value is the audience AWS will accept. It must match the aud claim in the token. The configure-aws-credentials action requests sts.amazonaws.com by default, so the two line up without extra work.
Check your account first. If the provider already exists, the command fails with an EntityAlreadyExists error, and you can reuse the existing provider.
Step 2: Create the role with a trust policy
Save this trust policy as trust-policy.json. It is the policy from the earlier chapter, and it accepts only tokens for the main branch of one repository.
{
"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"
}
}
}
]
}Three parts matter:
Principal.Federatedpoints at the provider you created. Replace the account ID with your own.Actionissts:AssumeRoleWithWebIdentity. A trust policy written forsts:AssumeRolewill not work here.- The
Conditionblock does the real access control. Without thesubcondition, any repository on GitHub could present a valid token and assume your role.
Create the role and attach a permissions policy:
aws iam create-role \
--role-name github-deploy \
--assume-role-policy-document file://trust-policy.json
aws iam attach-role-policy \
--role-name github-deploy \
--policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccessThe managed policy here is a stand-in. For a real deployment, write a policy that grants only the actions and resources the job needs.
Step 3: Write 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-identityNotes on this file:
id-token: writelets the job request an OIDC token. It does not give write access to your repository.- Setting
permissionsreplaces the default token permissions. That is whycontents: readappears. Without it,actions/checkoutfails in a private repository. - The action requests the token for you, sends it to AWS STS, and exports
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN, and the region as environment variables for later steps. - No secret is stored anywhere. The role ARN is an identifier, not a credential, so it can sit in the workflow file.
Step 4: Verify the identity
The last step prints the identity AWS sees. A successful run returns output like this:
{
"UserId": "AROAEXAMPLEID:GitHubActions",
"Account": "123456789012",
"Arn": "arn:aws:sts::123456789012:assumed-role/github-deploy/GitHubActions"
}The Arn shows an assumed-role for github-deploy, followed by a session name. The default session name is GitHubActions. You can set role-session-name in the action to something more useful, such as the run ID. That name appears in AWS CloudTrail, so a descriptive value makes audits easier.
What the temporary credentials look like
The credentials AWS returns are short-lived. By default, the role session lasts one hour. You can lower it with the action's role-duration-seconds input, within the maximum session duration set on the role. A shorter session limits the damage if a step leaks the credentials.
The action masks the credentials in logs. Masking is a safety net and not a guarantee, since a step that transforms a value, such as base64-encoding it, can defeat it. Avoid printing environment variables in the job.
Adjusting the subject condition
The subject you match depends on how the workflow is triggered. Pick the pattern that fits.
| Trigger or setup | Example `sub` value |
|---|---|
| Push to a branch | `repo:octo-org/octo-repo:ref:refs/heads/main` |
| Pull request | `repo:octo-org/octo-repo:pull_request` |
| Job using an environment | `repo:octo-org/octo-repo:environment:production` |
| Push of a tag | `repo:octo-org/octo-repo:ref:refs/tags/v1.0.0` |
When a job uses an environment:, the sub claim uses the environment form and no longer contains the branch. A trust policy written for ref:refs/heads/main will then reject the job. This catches many people who add an environment to a working workflow.
To allow several subjects, switch from StringEquals to StringLike and use a wildcard, or list several values in an array:
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": [
"repo:octo-org/octo-repo:ref:refs/heads/main",
"repo:octo-org/octo-repo:environment:production"
]
}
}Debugging failures
Most failures come from one of four causes. Read the error text in the action's log first.
| Symptom | Likely cause | Fix |
|---|---|---|
| The action reports it could not get an ID token, or the token request variables are missing | The job lacks `id-token: write`, or the workflow was triggered from a fork's pull request | Add the permission at workflow or job level. Fork pull requests do not receive tokens. |
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | The `sub` or `aud` condition does not match the token | Compare the actual claims to the trust policy. Check branch, environment, and repository name, including case. |
| `No OpenIDConnect provider found in your account` | The provider is missing in this account, or its URL is wrong | Create the provider with the exact URL, with no trailing slash. |
| `AccessDenied` on a later AWS call | Assuming the role worked, but the permissions policy is too narrow | Update the role's permissions policy. |
To see exactly what GitHub sent, decode your own token in a throwaway debugging job. The request from the earlier chapter returns the JWT, and the payload is its middle segment. Printing a token is acceptable for a short-lived debugging run, but remove the step afterward. The sub value in the decoded payload is the string your condition must match.
If the error is Not authorized, work through these checks in order:
- Confirm the
audin the token equals the client ID registered on the provider. - Confirm the
substring matches the trust policy character for character. - Confirm the role ARN in the workflow points to the account where the provider exists.
- Confirm the trust policy uses
sts:AssumeRoleWithWebIdentity.
Exercises
Exercise
A workflow that assumed its role successfully on main starts failing with "Not authorized to perform sts:AssumeRoleWithWebIdentity" after you add environment: production to the deploy job. The trust policy has sub set to repo:octo-org/octo-repo:ref:refs/heads/main. Why does it fail, and how do you fix it?
Show answer
When a job uses an environment, GitHub issues a token whose sub is repo:octo-org/octo-repo:environment:production, not the branch form. The trust policy condition no longer matches. Change the condition to accept the environment subject, either by replacing the value or by listing both values with StringLike.
Exercise
Why does the workflow need contents: read next to id-token: write, and why is id-token: write safe to grant without giving repository write access?
Show answer
Declaring a permissions block removes the default permissions, so contents: read must be added back for actions/checkout to work in private repositories. id-token: write only allows the job to request an OIDC token from GitHub. It does not grant any write access to repository contents.
Exercise
Name the action in the trust policy and explain why sts:AssumeRole is the wrong choice for a GitHub Actions role.
Show answer
The action is sts:AssumeRoleWithWebIdentity. The job authenticates with a web identity token (a JWT from an OIDC provider), not with existing AWS credentials, and sts:AssumeRole is the call used by principals that already have AWS credentials.