Sub topic 8
Allowing only the main branch
This chapter shows how to write a trust policy that lets only the main branch of one repository assume an AWS role. It assumes you know GitHub Actions workflow YAML, AWS IAM roles, and what a JSON Web Token (JWT) is. It also assumes the GitHub OIDC provider is already registered in your AWS account.
How the sub claim names a branch
When a workflow job runs without an environment key, GitHub puts the repository and Git ref in the token's sub claim:
repo:example-org/example-repo:ref:refs/heads/mainThe value has fixed parts: the repository, then ref:, then the full Git ref. A branch ref starts with refs/heads/. To trust only main, the trust policy must match this string exactly.
The trust policy
{
"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"
}
}
}
]
}Three parts matter:
Principalnames the OIDC provider, so only tokens issued by GitHub's provider are considered.- The
audcondition requires the audiencests.amazonaws.com, which is what the AWS action requests by default. - The
subcondition usesStringEquals, so the whole value must match. Any other repository, branch, tag, or pull request fails the check.
All conditions under one operator must be true. The token needs the right audience and the right subject.
The workflow side
The job needs permission to request a token. Without id-token: write, no token is created and the role cannot be assumed.
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-identityThis job has no environment key, so its sub ends in ref:refs/heads/main and matches the policy.
Exact match or wildcard
Sometimes you want a family of branches, such as every branch under release/. That needs StringLike:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/release/*"
}
}
}The * matches any characters, including /. Anyone who can push a branch named release/anything can now assume the role. Prefer StringEquals unless you need the pattern, and keep the fixed part of the pattern as long as possible.
Common mistakes
- Omitting the
subcondition. Then any repository on GitHub can assume the role if the audience matches. - Writing
repo:example-org/*. This trusts every repository in the organization, including ones created later. - Using
refs/heads/main*. This also matchesmain-experiment. - Forgetting that a job with an
environmentkey gets a differentsub(...:environment:production), so it will not match a branch-based policy. The next chapter covers that case.
Exercise
A trust policy uses StringEquals on the sub claim with the value repo:example-org/example-repo:ref:refs/heads/main. A developer pushes a branch named main-hotfix and runs the deploy workflow there. Does the role assumption succeed? Explain why.
Show answer
No. The sub claim for that run is repo:example-org/example-repo:ref:refs/heads/main-hotfix. StringEquals requires the whole value to match, and it does not match the policy value, so AWS denies the request.