Sub topic 12
Gating production with GitHub environments
This chapter assumes you can read a GitHub Actions workflow and an IAM trust policy. It also assumes you have already created the IAM OIDC provider for token.actions.githubusercontent.com and know what the aud and sub claims mean. Here you add a human approval step and a branch restriction in front of the production role.
Why branch conditions are not enough
A trust policy that matches repo:my-org/my-repo:ref:refs/heads/main proves the job runs on main. It does not prove a person agreed to deploy. Any commit merged to main can assume the role right away.
A GitHub environment is a named deployment target, such as staging or production, that you configure in the repository settings. It can hold protection rules that GitHub enforces before a job starts. If you tie the AWS role to that environment, AWS accepts the job only after GitHub has enforced those rules.
What an environment changes in the token
When a job declares environment: production, GitHub changes the sub claim in the token. The sub value no longer contains the branch or tag. It looks like this:
| Job setup | `sub` claim |
|---|---|
| No environment, push to `main` | `repo:my-org/my-repo:ref:refs/heads/main` |
| No environment, pull request | `repo:my-org/my-repo:pull_request` |
| `environment: production` | `repo:my-org/my-repo:environment:production` |
| `environment: staging` | `repo:my-org/my-repo:environment:staging` |
The environment form applies to every trigger, including pull_request. The ref is not part of sub for these jobs. A trust policy that matches environment:production therefore does not check the branch by itself. You enforce the branch inside GitHub, with a deployment branch rule.
The protection rules
Open the repository, then Settings, then Environments, and create production. These rules are available:
- Required reviewers. Up to six people or teams. One of them must approve before the job runs.
- Wait timer. A delay in minutes between the job being queued and starting. It gives people time to cancel.
- Deployment branches and tags. Restricts which refs can deploy to the environment. Select "Selected branches and tags" and add
main. - Environment secrets and variables. Values visible only to jobs that target the environment. You can store the role ARN here as a variable, so each environment points to its own role.
Some protection rules need a paid plan on private repositories. Check your plan before you rely on them.
GitHub applies the rules before the job starts. A job waiting for approval has not run any steps, so no OIDC token exists yet. The token is minted only after approval, when the job asks for it.
The trust policy for the production role
The trust policy matches the environment form of sub. Because the value has no wildcard, use StringEquals, which is stricter than StringLike.
{
"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:my-org/my-repo:environment:production"
}
}
}
]
}Only a job in my-org/my-repo that targets the production environment can use this role. A workflow that omits the environment key produces a different sub and is rejected by AWS.
A workflow with two environments
The workflow below deploys to staging automatically and to production after approval. Each environment uses a separate role, so a compromise of the staging job cannot reach production.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
staging:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy-staging
aws-region: us-east-1
- name: Deploy to staging
run: aws s3 sync ./site s3://my-example-bucket-staging --delete
production:
needs: staging
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/github-deploy-production
aws-region: us-east-1
- name: Deploy to production
run: aws s3 sync ./site s3://my-example-bucket-production --deleteThe production job waits for the staging job, then pauses for the required reviewers. The needs key orders the jobs. The approval gate comes from the environment, not from needs.
The staging role trusts repo:my-org/my-repo:environment:staging. If you want one role for several environments, sub accepts a list of values. Each environment you add widens the role, so separate roles are usually easier to audit.
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": [
"repo:my-org/my-repo:environment:staging",
"repo:my-org/my-repo:environment:qa"
]
}
}Closing the gaps
An environment is only as strong as its configuration in GitHub. Check these points.
- Set a deployment branch rule. The trust policy cannot see the branch, so without the rule, a job on any branch can declare
environment: production. If it has no required reviewers, nothing stops it. With the rule, GitHub refuses to run the job on other refs. - Require reviewers. A branch rule alone allows an unreviewed merge to
mainto deploy at once. - Prevent self-review. Enable "Prevent self-review" so the person who triggered the run cannot approve it.
- Protect the workflow file. Anyone who can edit
.github/workflowson an allowed branch can change which environment a job uses. Use branch protection and CODEOWNERS on that path. - Do not skip the
subcondition. If you trust onlyaud, any repository on GitHub can assume the role. Always matchsub.
If you need the ref and the environment together in one claim, GitHub lets a repository customize the sub claim to include extra fields. That changes every trust policy that depends on the default format, so plan the migration before you enable it.
Common failures
| Symptom | Likely cause |
|---|---|
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` after adding an environment | The trust policy still has the `ref:refs/heads/main` form. Change it to the `environment:` form. |
| The job never starts | It is waiting for approval or a wait timer. Check the run page for a pending review. |
| The job fails with a branch policy message | The ref is not in the environment's deployment branch rule. |
| Staging works and production fails | The production role trust policy has a typo in the environment name. Names are case sensitive. |
To confirm the exact value, you can add a temporary step that prints the identity from aws sts get-caller-identity. If the assumption fails, the error appears in the configure-aws-credentials step, and the CloudTrail event for AssumeRoleWithWebIdentity shows the sub that AWS received.
Exercise
A trust policy requires sub to equal repo:my-org/my-repo:environment:production. A developer pushes a branch named experiment and edits the workflow to add environment: production to a job. The environment has no deployment branch rule and no required reviewers. Does AWS grant credentials, and what two settings in GitHub would stop it?
Show answer
Yes. The sub claim for the job is repo:my-org/my-repo:environment:production whatever the branch is, so the trust policy matches and AWS grants credentials. A deployment branch rule that allows only main would make GitHub refuse to run the job. Required reviewers would pause the job until someone approves it, and with self-review prevented the developer could not approve their own run.
Exercise
Why does the production role in this chapter use StringEquals for sub, while the earlier branch example used StringLike?
Show answer
The environment value contains no wildcard, so exact matching is enough and is stricter. StringLike is only needed when the value includes * or ?.