Sub topic 7
Restricting access by repository, branch, and environment
This chapter assumes you can read an IAM trust policy and write a basic GitHub Actions workflow. It also assumes you have already registered token.actions.githubusercontent.com as an IAM OIDC provider. Here you will learn how to decide which workflows may assume a role, using conditions on the token's sub claim.
Why the sub condition is the real access control
Every GitHub Actions OIDC token is signed by the same issuer, whatever repository it came from. The provider and the Principal in your trust policy only establish that AWS trusts GitHub's signature. They don't establish that AWS trusts your repository.
If a trust policy has no condition on sub, any workflow in any GitHub repository can present a valid token and assume the role. Everything that narrows access to your code lives in the Condition block.
Two claims matter most:
audis the audience. Theconfigure-aws-credentialsaction sets it tosts.amazonaws.com. Check it withStringEquals.subis the subject. GitHub builds it from the repository and the context of the run. It is the claim you use to restrict access.
The shapes of the sub claim
GitHub builds the sub value from fixed patterns. The pattern depends on what triggered the job and whether the job uses an environment.
| Run context | `sub` value |
|---|---|
| Push or workflow run on a branch | `repo:my-org/my-repo:ref:refs/heads/main` |
| Push of a tag | `repo:my-org/my-repo:ref:refs/tags/v1.0.0` |
| Pull request event | `repo:my-org/my-repo:pull_request` |
| Job that declares an environment | `repo:my-org/my-repo:environment:production` |
A job that declares environment: gets the environment form of sub, even if it was triggered by a push to main. The branch is not part of that string. A condition that matches only ref:refs/heads/main will not match a job that uses an environment.
Restricting to one branch
The trust policy from the previous chapter limits the role to main of one repository:
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:ref:refs/heads/main"
}
}This is a good default for a deployment role. Feature branches and pull requests cannot assume it, so a contributor cannot edit a workflow on a branch and use it to reach your AWS account.
Use StringEquals when you want an exact match and no wildcards. StringLike supports * and ?. Matching is case-sensitive, so My-Org and my-org are different values. Copy the owner and repository names exactly as GitHub shows them.
Restricting to an environment
A GitHub environment is a named deployment target with its own rules. Those rules can require reviewers, add a wait timer, or limit which branches may deploy. If the role is tied to the environment, AWS access is granted only after those rules pass.
{
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:production"
}
}The workflow job must declare the same environment name:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy-prod
aws-region: us-east-1GitHub holds the job until the environment's protection rules are satisfied. The token is issued only after that, so a pending approval means no AWS credentials exist yet.
Environment names are matched literally. A workflow cannot claim production unless the repository has an environment with that name, but any user who can create environments in the repository can create one. Restrict who can administer the repository and its environments, and limit the environment's deployment branches in GitHub.
Allowing more than one context
To allow several subjects, give the condition a list. IAM treats the values as an OR.
{
"StringLike": {
"token.actions.githubusercontent.com:sub": [
"repo:my-org/my-repo:ref:refs/heads/main",
"repo:my-org/my-repo:environment:production"
]
}
}This is useful during a migration. Say some jobs deploy from main without an environment, and others use the production environment. The list covers both.
Wildcards and how far they reach
Wildcards trade precision for convenience. Compare these patterns:
| Pattern | What it allows |
|---|---|
| `repo:my-org/my-repo:ref:refs/heads/main` | One branch of one repository |
| `repo:my-org/my-repo:ref:refs/heads/release/*` | Any branch under `release/` in one repository |
| `repo:my-org/my-repo:*` | Every branch, tag, pull request, and environment in one repository |
| `repo:my-org/*` | Every repository in the organization, including ones created later |
Prefer the narrowest pattern that covers your real deployments. A broad pattern such as repo:my-org/* means that any repository in the organization can assume the role, including an experimental one that has weaker review rules.
Separate roles for separate risk
A single role with many allowed subjects gives every one of them the same permissions. A better pattern is one role per trust level:
- A
github-planrole, allowed from pull requests, with read-only permissions. - A
github-deploy-stagingrole, allowed frommain, with write access to staging. - A
github-deploy-prodrole, allowed only from theproductionenvironment, with write access to production.
Each role has its own trust policy and its own permissions policy, so a compromise of one context does not reach the others.
Common mistakes
- Using an environment in the workflow but matching a
ref:subject in the trust policy. The values do not match, and role assumption fails. - Matching
ref:refs/heads/mainwhen the default branch is namedmaster, or the reverse. - Using
StringEqualswith a wildcard.StringEqualstreats*as a literal character, so the condition never matches. - Writing the condition under the wrong key. The key must be
token.actions.githubusercontent.com:sub, with the provider URL and nohttps://.
Exercise
A deployment job in my-org/my-repo declares environment: staging and runs on a push to main. The role's trust policy allows only repo:my-org/my-repo:ref:refs/heads/main. Will the role assumption succeed? What is the smallest change that fixes it?
Show answer
It will fail. A job that declares an environment gets the subject repo:my-org/my-repo:environment:staging, not the branch form, so the condition does not match. Either change the trust policy to allow repo:my-org/my-repo:environment:staging, or add that value to a list alongside the branch subject. Alternatively, remove the environment: key from the job, but then the environment's protection rules no longer apply.