OIDC deployments to AWS/Azure/GCP
Deploy without storing cloud keys in secrets.
By Invicly Tech, , in Git, GitHub & GitHub Actions. About 9 minutes to read.
In short
With OIDC, a workflow proves its identity to a cloud provider using a short-lived token signed by GitHub. The cloud trusts that token and returns temporary credentials, so no long-lived keys are stored in secrets.
Explained simply
The old way to deploy from CI was to create a cloud access key, paste it into a GitHub secret, and hope it never leaks. Those keys often live for years. If one is copied from a log or a laptop, an attacker can use it until someone notices.
OIDC (OpenID Connect) replaces the stored key with a short-lived ID card. When a job needs cloud access, it asks GitHub for a signed token that says, in effect, "I am a workflow in repo acme/api, running on main, for the production environment". The token is valid only for a few minutes.
The cloud provider checks GitHub's signature and compares the details with a trust rule you wrote, like "only acme/api in the production environment may use this role". If they match, it hands back temporary credentials, usually valid for an hour.
So nothing permanent is stored in GitHub, there is nothing to rotate, and a stolen token is useless minutes later. AWS, Azure and Google Cloud all support this, and each has an official login action that does the exchange for you.
Press Play on a diagram to walk through it one step at a time.
- Workflow job to GitHub OIDC: Request ID token1Request ID token
- GitHub OIDC to Workflow job: Signed JWT with repo and ref2Signed JWT with repo and ref
- Workflow job to AWS STS: AssumeRoleWithWebIdentity3AssumeRoleWithWebIdentity
- AWS STS to Workflow job: Trust policy OK: temp credentials4Trust policy OK: temp credentials
- Workflow job to AWS service: Deploy using temporary creds5Deploy using temporary creds
- Created by hand, live for months
- Stored as GitHub secrets
- Anyone who reads it can use it anywhere
- Must be rotated and audited
- Token minted per job
- Nothing stored in GitHub
- Only works for matching repo and environment
- Credentials expire on their own
In depth
What is inside the token
The ID token is a JWT (JSON Web Token): a small JSON document signed by GitHub. Its issuer (iss) is https://token.actions.githubusercontent.com, and GitHub publishes the public keys at that address so any cloud can verify the signature. The audience (aud) says who the token is for, such as sts.amazonaws.com for AWS.
The claims describe the run: repository, repository_owner, ref, event_name, environment, actor, workflow and job_workflow_ref (the exact reusable workflow file and ref), plus numeric IDs like repository_id. The most important claim is sub (subject), which combines the repo with the branch, tag, environment or pull request. Trust rules usually match on sub and aud.
- iss
- https://token.actions.githubusercontent.com
- aud
- sts.amazonaws.com for AWS
- Set per cloud or action
- sub
- repo:acme/api:environment:production
- repo:acme/api:ref:refs/heads/main
- Context claims
- repository, ref, event_name
- environment, actor
- job_workflow_ref
- Stable IDs
- repository_id
- repository_owner_id
How the sub claim is built
The format depends on the job. If the job uses an environment, sub is repo:OWNER/REPO:environment:NAME, and the branch is not part of it. Otherwise, for a branch push it is repo:OWNER/REPO:ref:refs/heads/BRANCH, for a tag repo:OWNER/REPO:ref:refs/tags/TAG, and for a pull_request event repo:OWNER/REPO:pull_request.
This matters when you write trust rules. If your deploy job has environment: production, a rule that expects ref:refs/heads/main never matches. Matching on the environment is usually best, because the environment's own branch rules and reviewers then also guard the cloud role. Organizations can customise the sub template through the API, for example to add repository_id, which protects against someone recreating a deleted repo with the same name.
- Environment job: repo:acme/api:environment:production
- Branch: repo:acme/api:ref:refs/heads/main
- Tag: repo:acme/api:ref:refs/tags/v1.2.0
- Pull request: repo:acme/api:pull_request
Setting it up on AWS, Azure and Google Cloud
AWS: create an IAM OIDC identity provider for https://token.actions.githubusercontent.com with audience sts.amazonaws.com, then an IAM role whose trust policy allows sts:AssumeRoleWithWebIdentity with conditions on the aud and sub claims. Use aws-actions/configure-aws-credentials with role-to-assume. Sessions last one hour by default (role-duration-seconds changes it).
Azure: add a federated credential to an app registration or a user-assigned managed identity, with the GitHub issuer, the exact subject and audience api://AzureADTokenExchange, then use azure/login with client-id, tenant-id and subscription-id. Google Cloud: create a Workload Identity Pool with an OIDC provider, map claims such as google.subject=assertion.sub and attribute.repository=assertion.repository, add an attribute condition that limits it to your organization, and use google-github-actions/auth.
- IAM OIDC identity provider
- Role trust policy on aud and sub
- aud: sts.amazonaws.com
- configure-aws-credentials
- Federated credential on app or identity
- Exact subject match
- aud: api://AzureADTokenExchange
- azure/login
- Workload Identity Pool and provider
- Attribute mapping and condition
- Grant roles to the pool principal
- google-github-actions/auth
Permissions and other gotchas
The job needs permissions: id-token: write. But once you write a permissions block, every permission you do not list becomes none. So add contents: read as well, or actions/checkout fails on a private repo. In a reusable workflow, the caller must grant id-token: write, because a called workflow cannot raise permissions.
Pull requests from forks never get id-token: write, so they cannot reach your cloud, which is what you want. Be careful with wildcards: a trust condition like repo:acme/* lets every repo in the org, including any new one, assume the role. Give each role only the cloud permissions that one deployment needs, and use separate roles for staging and production.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Learn how OIDC tokens replace keys~45m
- 2Configure a cloud trust policy~45m
- 3Request id-token permission in workflows~45m
- 4Deploy to the cloud with no stored secrets~45m
Key points
- The job needs permissions: id-token: write to request an OIDC token.
- The cloud trust policy should match the sub claim to your repo and branch or environment.
- Credentials expire on their own, so there is nothing to rotate or leak.
- AWS, Azure and Google Cloud all support this via official login actions.
Key terms
Tap a card to flip it. 0/5 known
Examples
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-deploy
aws-region: us-east-1
- run: aws s3 sync ./site s3://acme-site --deleteNo AWS keys in secrets: the role is assumed via OIDC.
An AWS trust policy locked to one environment
Only the production environment of acme/api may deploy. Step 1: the Principal is the GitHub OIDC provider in your account. Step 2: StringEquals checks the audience is sts.amazonaws.com. Step 3: StringEquals checks sub is exactly the production environment of this repo; because the job uses environment: production, the branch is not in sub. Step 4: in the workflow, grant id-token: write and pass this role's ARN to configure-aws-credentials.
{
"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:acme/api:environment:production"
}
}
}]
}Deploy to Cloud Run with Workload Identity Federation
The same idea on Google Cloud. Step 1: permissions grant id-token: write and contents: read. Step 2: google-github-actions/auth exchanges the GitHub token using your pool provider and impersonates a deployer service account. Step 3: setup-gcloud installs the CLI, which picks up the temporary credentials. Step 4: gcloud deploys the image built for this commit. No JSON key file is stored anywhere.
permissions: { contents: read, id-token: write }
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: google-github-actions/auth@v2
with:
workload_identity_provider: projects/123456789/locations/global/workloadIdentityPools/github/providers/acme
service_account: deployer@acme-prod.iam.gserviceaccount.com
- uses: google-github-actions/setup-gcloud@v2
- run: gcloud run deploy api --image "$IMAGE" --region europe-west1
env: { IMAGE: "europe-west1-docker.pkg.dev/acme-prod/app/api:${{ github.sha }}" }Quick reference
- OIDC needs permissions: id-token: write; add contents: read for checkout
- Issuer: https://token.actions.githubusercontent.com; AWS audience: sts.amazonaws.com
- With environment: set, sub is repo:OWNER/REPO:environment:NAME
- Without an environment, sub is repo:OWNER/REPO:ref:refs/heads/BRANCH
- Azure federated credentials use audience api://AzureADTokenExchange
- Never trust repo:ORG/* in a production role; match one repo and environment
- Forked pull requests cannot get an OIDC token
- AWS sessions from configure-aws-credentials last 1 hour by default
Common mistakes
- A trust policy matching repo:acme/* lets every repo in the org assume the role.
- Forgetting id-token: write, which makes the token request fail.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write the permissions block for a job that checks out a private repo and logs in to AWS with OIDC, with nothing extra.
- Exercise 2medium
Change the AWS trust policy so that pushes to main and any tag starting with v can assume the role, but nothing else.
- Exercise 3hard
Your role assumption fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". Print the token's sub and aud claims in a debug run without printing the token itself.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Do I still need any secrets with OIDC?
- Not for the cloud credentials. You pass identifiers such as a role ARN or client ID, which are not secret, though many teams store them as variables for convenience.
- Does OIDC work with self-hosted runners?
- Yes. The token comes from GitHub's service, not from the runner, so any runner can request it when the job has id-token: write.
- How long do the credentials last?
- The GitHub token itself is short-lived. The cloud credentials depend on the provider and settings; on AWS the default session from configure-aws-credentials is one hour.
- Can I use OIDC with services other than the big three clouds?
- Yes. Vault, PyPI trusted publishing, npm trusted publishing and many other services accept GitHub's OIDC tokens.