Sub topic 9
Allowing only a deployment environment
This chapter assumes you know GitHub Actions workflow YAML, AWS IAM roles and trust policies, and what a JSON Web Token (JWT) is. You should also have an OIDC identity provider for token.actions.githubusercontent.com in your AWS account.
Why restrict by environment
A branch restriction answers "which code ran?". An environment restriction answers "which deployment target did this job request?". In GitHub Actions, an environment is a named target such as production. It can have its own protection rules, such as required reviewers.
When a job declares environment: production, GitHub changes the sub claim in the OIDC token. The default format becomes:
repo:example-org/example-repo:environment:productionThe branch no longer appears in sub. Once a job names an environment, the environment replaces the ref in the subject.
The trust policy
This trust policy accepts only jobs that target the production environment of example-org/example-repo.
{
"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"
}
}
}
]
}Both conditions use StringEquals, so the values must match exactly. The aud condition makes sure the token was issued for AWS STS. The sub condition pins the repository and the environment.
The matching workflow
The workflow must do two things. It needs id-token: write so the job can request an OIDC token. It also needs the environment key so the sub claim ends in environment:production.
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-identityThe final step prints the assumed role's identity. If the trust policy and the sub claim agree, you see the role ARN. If they disagree, the action fails with an AssumeRoleWithWebIdentity error.
How the check flows
Environments add a second gate
The trust policy is one gate. Environment protection rules are another. If production requires a reviewer, GitHub holds the job until someone approves it. The job gets no token until then. A person has to approve before the role can be assumed.
Environment rules can also limit which branches may deploy to the environment. This matters because the trust policy above does not check the branch. Without a deployment branch rule, a workflow on any branch that names environment: production produces a matching sub value. Configure the environment so only main can deploy to it.
Common mistakes
- Using
StringLikewith a wildcard such asenvironment:*. This accepts every environment, including ones you create later. - Forgetting
environment:in the workflow. Thesubis then branch-based, and the role is not assumed. - Omitting the
audcondition, which leaves the audience unchecked.
Exercise
A job declares environment: production and runs on the main branch. The role's trust policy requires sub to equal repo:example-org/example-repo:ref:refs/heads/main. Does the job assume the role? Why?
Show answer
No. Because the job names an environment, the sub claim is repo:example-org/example-repo:environment:production, not the branch form. The exact match fails. Change the trust policy to the environment subject, or remove the environment key from the job.