Sub topic 14
Scoping cloud credentials with OIDC and environments
This chapter assumes you can read workflow YAML, know how a job targets an environment, and understand why long-lived cloud keys stored as secrets are a risk. It covers how OpenID Connect (OIDC) lets a job get short-lived cloud credentials, and how an environment narrows who can get them.
Why OIDC instead of stored keys
A stored cloud access key is valid until someone rotates it. Anyone who can read the secret can use it from anywhere.
With OIDC, no cloud key is stored in GitHub. Each job asks GitHub for a signed token that describes the job. The cloud provider checks the token against a trust rule you wrote. If the claims match, the provider returns credentials that expire in minutes or hours.
The subject claim
The token carries a sub (subject) claim. Its format depends on how the job runs:
| Job setup | Example `sub` value |
|---|---|
| Job with `environment: production` | `repo:OWNER/REPO:environment:production` |
| Job on the `main` branch, no environment | `repo:OWNER/REPO:ref:refs/heads/main` |
When a job uses an environment, the environment name appears in sub. Your cloud trust rule can then require environment:production. A job that does not target that environment cannot assume the role, even if it runs in the same repository.
A workflow that uses OIDC
name: Deploy with OIDC
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/gha-production-deploy
aws-region: us-east-1
- run: aws sts get-caller-identityTwo settings matter here:
id-token: writeallows the job to request an OIDC token. Without it, the token request fails.environment: productionputs the environment name into the token'ssubclaim.
Because the job targets production, it also waits for that environment's protection rules. Required reviewers must approve before the job starts, so the token is only requested after approval.
Common errors
- Missing
id-token: write. The credentials step fails because no token can be requested. Set the permission at the workflow or job level. - Trust rule does not match
sub. The cloud provider rejects the token. Compare the exactsubstring, including the environment name and its capitalization, with the condition in your trust policy. - Trust rule scoped only to the repository. Any workflow in the repository can assume the role, including jobs that skip approval. Require the environment in the condition.
- Permissions set too narrowly. Setting
permissionsat the top level removes defaults for other scopes. Listcontents: readif the job checks out code.
Exercise
A job targets environment: production and has id-token: write. Which part of the OIDC token lets the cloud role be limited to production, and where is that limit enforced?
Show answer
The sub claim includes the environment name, such as repo:OWNER/REPO:environment:production. The limit is enforced in the cloud provider's trust rule for the role, which must require that sub value.