Goodbye Access Keys: Connecting GitHub Actions to AWS with OIDC
Replace long-lived AWS keys in GitHub secrets with short-lived OIDC credentials. IAM setup, trust policies, a working workflow, Terraform and a troubleshooting guide.
- Author
- Navinda Chandrasiri
- Published
- Reading time
- 9 min read

On this page
- Why access keys in GitHub secrets are a liability
- How the OIDC handshake works
- Step 1: Create the GitHub identity provider in AWS
- Step 2: Create the IAM role and its trust policy
- Choosing the right sub value
- Step 3: Attach a least-privilege permissions policy
- Step 4: Use the role in a GitHub Actions workflow
- Step 5: Verify it works
- The same setup as Terraform
- Hardening checklist
- Troubleshooting
- Wrapping up
Somewhere in your GitHub organisation there is probably a secret called AWS_ACCESS_KEY_ID. It was created "just for CI", it has more permissions than the pipeline needs, and nobody remembers when it was last rotated. If it leaks, through a compromised dependency, a verbose log or a former teammate's laptop, it keeps working until somebody notices.
OpenID Connect (OIDC) removes that whole class of problem. Instead of storing a password for AWS inside GitHub, you let AWS trust GitHub itself. Every workflow run gets a short-lived credential that only exists for that job, and only for the repository, branch or environment you allowed.
This guide walks through the full setup: the identity provider, the IAM role and its trust policy, a least-privilege permissions policy, the workflow, the same thing in Terraform, and what to check when it breaks.
Why access keys in GitHub secrets are a liability#
Static keys have four problems that no amount of discipline fully fixes:
- They do not expire. A key stolen today works next year unless someone rotates it.
- They are not tied to a context. The key does not know whether it is being used by your
mainbranch or by a pull request someone opened an hour ago. - They spread. Copies end up in local
.envfiles, old forks, build logs and chat messages. - Rotation is manual work, so it does not happen.
OIDC flips each of these. Credentials last about an hour at most, they are issued for a specific workflow run, and there is no secret to copy because nothing secret is stored.
How the OIDC handshake works#
You do not need to implement any of this yourself, but understanding it makes the trust policy much easier to write correctly.
- Your workflow job asks GitHub for an ID token, a signed JWT that describes the run: which repository, which branch, which environment, which workflow.
- The job sends that token to AWS STS using
AssumeRoleWithWebIdentity, naming the IAM role it wants. - AWS checks the token's signature against the OIDC identity provider you registered, then evaluates the role's trust policy against the token's claims.
- If everything matches, STS returns temporary credentials. The job uses them like any other AWS credentials, and they expire on their own.
Two claims matter most:
aud(audience) is who the token is meant for. For AWS this issts.amazonaws.com.sub(subject) identifies the workflow context, for examplerepo:my-org/my-repo:ref:refs/heads/main.
The identity provider is shared by every GitHub repository on the planet. The
subcondition in your trust policy is the only thing stopping a stranger's repository from assuming your role.
Read that again before writing your first trust policy.
Step 1: Create the GitHub identity provider in AWS#
You only do this once per AWS account.
In the console, open IAM, then Identity providers, then Add provider, and choose OpenID Connect. Use these values:
- Provider URL:
https://token.actions.githubusercontent.com - Audience:
sts.amazonaws.com
Or with the AWS CLI:
aws iam create-open-id-connect-provider \
--url https://token.actions.githubusercontent.com \
--client-id-list sts.amazonaws.comA note on thumbprints: older guides tell you to hunt for a certificate thumbprint. AWS now verifies GitHub's certificate against its own library of trusted root CAs, so you can ignore it. If an older CLI or tool still insists on a value, use the thumbprint GitHub publishes in its documentation.
Step 2: Create the IAM role and its trust policy#
Create a role (for example github-actions-deploy) with Web identity as the trusted entity. Then replace its trust policy with the one below. Replace the account ID, organisation and repository with your own.
{
"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:ref:refs/heads/main"
}
}
}
]
}What each part does:
- Principal says only tokens issued by the GitHub provider in this account may even try.
- Action is the web-identity flavour of
AssumeRole. - aud makes sure the token was minted for AWS and not for some other service.
- sub pins the role to one repository and one branch. This is the line that keeps the role private to you.
Choosing the right sub value#
The sub claim changes depending on what triggered the job:
| Trigger | Subject (sub) value |
|---|---|
| Push to a branch | repo:my-org/my-repo:ref:refs/heads/main |
| Push of a tag | repo:my-org/my-repo:ref:refs/tags/v1.0.0 |
| Pull request | repo:my-org/my-repo:pull_request |
| Job that uses a GitHub Environment | repo:my-org/my-repo:environment:production |
Notice that last row: once a job declares environment: production, the subject switches to the environment form regardless of which branch it runs on. That is exactly what you want for production, because the environment can add required reviewers and branch rules on the GitHub side, and the AWS side only trusts jobs that passed them.
If you genuinely need a wildcard (for example every branch of one repository), use StringLike and keep the wildcard as narrow as possible:
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:*"
}Never write repo:my-org/* unless you really mean "any repository in my organisation, including the one a new intern created last week".
Step 3: Attach a least-privilege permissions policy#
The trust policy decides who can become the role. The permissions policy decides what the role can do. Keep it as small as the job requires.
Here is an example for a pipeline that uploads a static site to S3 and invalidates a CloudFront distribution:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListSiteBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-site-bucket"
},
{
"Sid": "WriteSiteObjects",
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:DeleteObject"],
"Resource": "arn:aws:s3:::my-site-bucket/*"
},
{
"Sid": "InvalidateCdnCache",
"Effect": "Allow",
"Action": "cloudfront:CreateInvalidation",
"Resource": "arn:aws:cloudfront::123456789012:distribution/E1234567890ABC"
}
]
}Start narrow and widen only when a deploy fails with an AccessDenied message that tells you exactly which action is missing. It is much easier to add one permission than to discover later that a CI role has been able to delete your database all along.
Step 4: Use the role in a GitHub Actions workflow#
Two things are non-negotiable in the workflow: the id-token: write permission, and the official configure-aws-credentials action.
name: Deploy site
on:
push:
branches: [main]
permissions:
id-token: write # lets the job request an OIDC token
contents: read # lets actions/checkout read the repository
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-actions-deploy
aws-region: ap-southeast-1
- name: Who am I?
run: aws sts get-caller-identity
- name: Upload site
run: aws s3 sync ./dist s3://my-site-bucket --delete
- name: Invalidate CDN cache
run: aws cloudfront create-invalidation --distribution-id E1234567890ABC --paths "/*"Without id-token: write the job cannot request a token at all, which is the single most common reason a first attempt fails. Check the latest major versions of both actions when you adopt this, and see the hardening section below about pinning them.
Step 5: Verify it works#
Run the workflow and open the Who am I? step. You should see something like this:
{
"UserId": "AROAXXXXXXXXXXXXXXXXX:GitHubActions",
"Account": "123456789012",
"Arn": "arn:aws:sts::123456789012:assumed-role/github-actions-deploy/GitHubActions"
}assumed-role in the ARN confirms you are using temporary credentials. GitHubActions is the default session name, and you can change it with the role-session-name input to include the run ID, which helps when you audit CloudTrail later.
To prove the guard rails work, do a negative test: run the same workflow from a different branch, or from another repository, and confirm that it is rejected with Not authorized to perform sts:AssumeRoleWithWebIdentity. A trust policy you have never seen fail is a trust policy you have not tested.
The same setup as Terraform#
If you manage infrastructure as code, this is the entire setup in a few resources. Recent versions of the AWS provider no longer require a thumbprint list for this provider.
variable "github_repo" {
description = "GitHub repository in owner/name form"
type = string
default = "my-org/my-repo"
}
resource "aws_iam_openid_connect_provider" "github" {
url = "https://token.actions.githubusercontent.com"
client_id_list = ["sts.amazonaws.com"]
}
data "aws_iam_policy_document" "github_trust" {
statement {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [aws_iam_openid_connect_provider.github.arn]
}
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:aud"
values = ["sts.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:${var.github_repo}:ref:refs/heads/main"]
}
}
}
resource "aws_iam_role" "github_deploy" {
name = "github-actions-deploy"
assume_role_policy = data.aws_iam_policy_document.github_trust.json
max_session_duration = 3600
}Attach your permissions policy to aws_iam_role.github_deploy with aws_iam_role_policy and you are done.
Hardening checklist#
Once the basics work, tighten the screws:
- Pin
subexactly. PreferStringEqualsover wildcards, and never trust a whole organisation. - Gate production with a GitHub Environment. Use required reviewers and a branch restriction, and trust
environment:productionin AWS. - Use separate roles. One role per environment or account, and a read-only role for jobs that only need to plan or diff.
- Keep sessions short. The default of one hour is plenty for most pipelines.
- Start workflows with no permissions. Set
permissions: {}at the top and grantid-token: writeonly to the jobs that need it. - Pin third-party actions to a full commit SHA instead of a moving tag, and let Dependabot update them.
- Watch CloudTrail. Search for
AssumeRoleWithWebIdentityevents and alert on anything that does not come from the repositories you expect. - Delete the old keys. After migrating, remove the IAM user access keys and the GitHub secrets. A leftover key defeats the point.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
Credentials could not be loaded or Could not load credentials from any providers | The job has no id-token: write permission | Add it at the workflow or job level |
Not authorized to perform sts:AssumeRoleWithWebIdentity | The sub or aud in the trust policy does not match the token, or the provider ARN or account is wrong | Print the real claims (see below) and compare character by character |
No OpenIDConnect provider found in your account | The identity provider was created in a different AWS account | Create the provider in the account that owns the role |
Works on main, fails on a pull request | The pull request sub is repo:owner/repo:pull_request, not a branch | Add a separate, more restricted role for pull requests |
| Fails only on pull requests from forks | GitHub does not issue OIDC tokens to workflows triggered from forks | Skip the AWS job for forks, or deploy only after merge |
| Fails when the job declares an environment | The sub changed to environment:name | Update the trust policy to the environment form |
The fastest way to debug a sub mismatch is to look at the claims the job is actually sending. This step decodes the token and logs only the two values you need. It never prints the token itself.
- name: Show OIDC claims (debug only)
uses: actions/github-script@v7
with:
script: |
const token = await core.getIDToken('sts.amazonaws.com');
const claims = JSON.parse(Buffer.from(token.split('.')[1], 'base64url').toString());
core.info(`sub = ${claims.sub}`);
core.info(`aud = ${claims.aud}`);Remove it once the pipeline works. Case matters, so My-Org and my-org are different strings as far as IAM is concerned.
Wrapping up#
OIDC is one of those rare security upgrades that also makes life easier. There is nothing to rotate, nothing to leak from a settings page, and the access rules live next to the role they protect. The setup takes less than an hour, and you can migrate one repository at a time.
If you want to see this pattern applied to a real application, the next post builds a complete, locked-down Next.js deployment on top of it with SST, OpenNext, CloudFront and S3: Zero Stored Secrets: Deploying Next.js to AWS with SST, OpenNext and GitHub OIDC.
Related reading
Zero Stored Secrets: Deploying Next.js to AWS with SST, OpenNext and GitHub OIDC
A security-first pipeline for Next.js on AWS: GitHub Actions with OIDC, SST and OpenNext on S3, CloudFront and Lambda, environment gates, scoped IAM and safe PR previews.
Next.jsAWSSSTOpenNext
13 min readZero to Zipwyn: Building a Production Ready Serverless API on AWS with Terraform
Build a serverless REST API with AWS API Gateway, Lambda, DynamoDB, CloudWatch, and IAM using Terraform. Explore Zipwyn’s architecture, security, monitoring, and local development.
AWSServerlessTerraformAPI Gateway
13 min readThe GitHub Repository Playbook: Branching, Rulesets, Security, Releases and Recovery for a DevOps Project
A practical playbook for running a project on GitHub: branching, rulesets, security scanning, versioned releases, issue management, backups and disaster recovery, docs and standards.
GitHubDevOpsSecurityGit
24 min read