Sub topic 9
Allowing Only a Deployment Environment
This chapter assumes you can read GitHub Actions workflow YAML, you know what an IAM role and a trust policy are, and you know a JSON Web Token (JWT) carries claims. It builds on the earlier chapters about the sub and aud claims and on exact versus wildcard matching.
Why restrict by environment
A branch restriction answers the question "which code is running?" An environment restriction answers a different one: "which deployment target is this job allowed to touch?" In GitHub Actions, an environment is a named deployment target such as staging or production. A job opts in by setting environment: in the workflow. GitHub then evaluates the environment's protection rules before the job starts.
When a job declares an environment, GitHub changes the sub claim in the OIDC token. The default format becomes:
repo:<owner>/<repo>:environment:<environment-name>The branch is no longer part of sub. This matters: once a job uses an environment, a trust policy that matches ref:refs/heads/main will not match that job, and a policy that matches the environment will not match jobs without one.
What the token looks like
For a job in example-org/example-repo that targets production, the relevant claims are:
| Claim | Example value |
|---|---|
| `aud` | `sts.amazonaws.com` |
| `sub` | `repo:example-org/example-repo:environment:production` |
| `environment` | `production` |
| `ref` | `refs/heads/main` |
| `repository` | `example-org/example-repo` |
The ref claim is still present in the token. Only the sub claim drops the branch. AWS trust policies can match sub and aud for GitHub's provider, so the policy below uses sub.
The trust policy
This policy lets only jobs that target the production environment of example-org/example-repo assume the role. It also pins the audience.
{
"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:environment:production"
}
}
}
]
}Three details are worth checking:
- The condition operator is
StringEquals, so the match is exact. The environment nameproductionwill not matchproduction-euorProduction. - Both conditions are in the same operator block, so both must be true.
- The
Principalis the OIDC provider, not a role or account. Any workflow that can present a token from that provider reaches this policy, and the conditions are the only thing that filters them.
The matching workflow
The workflow must request an OIDC token and declare the environment. Without environment: production, the sub claim would be a branch subject and the trust policy would reject the job.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
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-identityid-token: write lets the job request a token. It does not grant any write access to the repository. contents: read is listed explicitly because setting any permissions key turns off the defaults for the other scopes.
The last step prints the assumed role's identity. If the trust policy matches, the output shows an assumed-role/example-deploy-role ARN. If it does not match, the credentials step fails with an error such as Not authorized to perform sts:AssumeRoleWithWebIdentity.
How the pieces fit together
The environment is checked twice, by two different systems. GitHub decides whether the job may start. AWS decides whether the token it presents may assume the role.
The first check depends on the environment's configuration in GitHub. The second depends on the trust policy. Neither replaces the other.
What the environment restriction does and does not protect
Setting environment: production in a workflow is something any contributor with write access can do, because workflow files live in the repository. The sub claim therefore only says "this job asked for production." The protection comes from the environment's rules in GitHub settings:
- Required reviewers pause the job until a named person or team approves it.
- Deployment branch and tag rules limit which refs can target the environment, for example only
main. - Wait timers delay the job by a set number of minutes.
If the environment has no rules, anyone who can push a branch can add environment: production to a workflow on that branch and receive a token with the production subject. The trust policy would accept it. In that case the environment restriction adds no real protection.
A related detail: if a workflow references an environment that does not exist yet, GitHub creates it automatically with no protection rules. A typo such as environment: prod creates an unprotected prod environment. That token would not match the production trust policy, so the failure is safe, but it shows why unprotected environments are easy to create by accident.
Combining a branch rule and an environment
The sub claim cannot carry both the environment and the branch in its default format. To require both, you have two options:
- Set a deployment branch rule on the environment in GitHub. GitHub then refuses to run jobs for other refs, so the trust policy can match only the environment.
- Customize the
subclaim template so it includes more claims. A later chapter covers this.
Option 1 is the simpler choice and keeps the trust policy short. The cost is that the branch restriction lives in GitHub settings, outside the IAM policy, so someone reading only the role cannot see it.
Supporting several environments
You may have staging and production roles. Prefer one role per environment, each with its own exact subject, and give each role only the permissions that environment needs. This keeps a staging compromise from reaching production.
If one role must trust two environments, use a list of exact values rather than a wildcard:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": [
"repo:example-org/example-repo:environment:staging",
"repo:example-org/example-repo:environment:production"
]
}
}
}A list under StringEquals is an OR across its values, so the token matches if sub equals either one. A pattern such as environment:* would also match any environment someone creates later.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Policy matches `ref:refs/heads/main` but the job sets `environment:` | Role assumption fails, because `sub` no longer contains the branch | Match the environment subject instead |
| Using `StringLike` with `environment:*` | Any environment, including new unprotected ones, is trusted | Use `StringEquals` with exact names |
| Omitting the `aud` condition | Tokens issued for another audience may be accepted | Keep `aud` equal to `sts.amazonaws.com` |
| No protection rules on the environment | Any branch can request the production subject | Add branch rules and reviewers |
| Wrong case or spelling in the name | Role assumption fails | Copy the name from the environment settings |
| Missing `id-token: write` | The job cannot obtain a token | Add the permission at workflow or job level |
Checking your work
Run the workflow and read the output of aws sts get-caller-identity. To test the negative case, create a branch, change the environment: value to a different name, and confirm that the credentials step is denied. Also confirm that a workflow without any environment is denied. Both denials show that the policy is matching the exact subject and not a looser pattern.
Exercise
A job declares environment: production and runs on main. Your trust policy requires sub to equal repo:example-org/example-repo:ref:refs/heads/main. Will the role assumption succeed, and why?
Show answer
No. When a job declares an environment, the sub claim becomes repo:example-org/example-repo:environment:production, which does not contain the branch. The StringEquals condition on the branch subject fails, so STS denies the request. Change the policy to match the environment subject, and use deployment branch rules on the environment to limit it to main.
Exercise
Your production environment has no protection rules. A teammate pushes a new branch whose workflow sets environment: production and the trust policy matches environment:production exactly. Can the branch's job assume the production role? What do you change?
Show answer
Yes. The exact match is satisfied because the job declares the environment, and GitHub applies no rule that stops it. Add a deployment branch rule that allows only main, and add required reviewers for production, so that GitHub blocks or pauses jobs from other branches before a token is issued.