Stop Putting AWS Keys in GitHub Secrets: Deploy with OIDC in 20 Minutes

Somewhere in your GitHub repo right now, there might be a secret called AWS_ACCESS_KEY_ID.
It was created "just to get the pipeline working." It belongs to an IAM user with more permissions than anyone remembers granting. It has never been rotated. Nobody knows who created it, and everyone is afraid to delete it.
Let's get rid of it. GitHub Actions can talk to AWS using short-lived credentials, with no stored keys at all. It's called OIDC, it takes about 20 minutes, and by the end you'll have a deploy pipeline you can actually sleep next to.
This is a hands-on post. Copy, paste, adapt. I've kept the theory to three sentences.
The idea in three sentences
When a workflow runs, GitHub can hand it a signed token that says "I am a job from repo X, on branch Y." AWS can be told to trust GitHub's signature and swap that token for temporary credentials, but only if the token matches the conditions you set. No long-lived key exists, so there's nothing to leak, rotate or forget about.
What you need
- An AWS account and permission to create IAM resources
- A GitHub repo with a workflow you want to deploy from
- The AWS CLI (or just the console, if you prefer clicking)
I'll use a common example: a static site built into ./out and deployed to an S3 bucket behind CloudFront. The same steps work for any AWS target, because only the permissions policy changes.
Step 1: Tell AWS to trust GitHub
You do this once per AWS account. In IAM, add an OpenID Connect identity provider with:
- Provider URL:
https://token.actions.githubusercontent.com - Audience:
sts.amazonaws.com
Or with the CLI:
aws iam create-open-id-connect-provider \
--url https://token.actions.githubusercontent.com \
--client-id-list sts.amazonaws.com
If the provider already exists in your account, you'll get an error saying so. That's fine, just reuse it.
Step 2: Create the role GitHub will assume
This is the important part, so go slowly. The role's trust policy decides which repos and branches are allowed in. Save this as trust-policy.json, replacing the placeholders:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::<ACCOUNT_ID>: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:<YOUR_ORG>/<YOUR_REPO>:ref:refs/heads/main"
}
}
}
]
}
Create the role:
aws iam create-role \
--role-name github-deploy-site \
--assume-role-policy-document file://trust-policy.json
Do not skip the sub condition. That line is the lock on the door. Without it, any repo on GitHub could ask to assume your role. With it, only workflows running on main in your repo can. That one line is the whole security model.
Step 3: Give the role only what it needs
Now say what the role is allowed to do. For our S3 + CloudFront example, that's very little. Save as deploy-policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:DeleteObject"],
"Resource": "arn:aws:s3:::my-site-bucket/*"
},
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-site-bucket"
},
{
"Effect": "Allow",
"Action": "cloudfront:CreateInvalidation",
"Resource": "arn:aws:cloudfront::<ACCOUNT_ID>:distribution/<DISTRIBUTION_ID>"
}
]
}
Attach it:
aws iam put-role-policy \
--role-name github-deploy-site \
--policy-name deploy-site \
--policy-document file://deploy-policy.json
Notice what's not there: no s3:*, no * resource, no admin. If this role ever misbehaves, the worst it can do is mess up one bucket.
Step 4: The workflow
Add .github/workflows/deploy.yml:
name: Deploy site
on:
push:
branches: [main]
permissions:
id-token: write # lets the job request the OIDC token
contents: read # lets the job check out your code
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 # use the current major version
# ... your build step here, producing ./out ...
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6.2.4
with:
role-to-assume: arn:aws:iam::<ACCOUNT_ID>:role/github-deploy-site
role-session-name: gh-${{ github.run_id }}
aws-region: ap-south-1 # your region
- name: Who am I?
run: aws sts get-caller-identity
- name: Upload to S3
run: aws s3 sync ./out s3://my-site-bucket --delete
- name: Clear the CDN cache
run: aws cloudfront create-invalidation --distribution-id <DISTRIBUTION_ID> --paths "/*"
Two small touches worth copying:
- The
id-token: writepermission is what makes the whole thing work. Forget it and the job can't ask for a token. - Setting
role-session-nameto the run ID means that when you read CloudTrail later, you can tie every AWS action back to the exact workflow run that did it.
Push to main. The "Who am I?" step should print an assumed-role ARN for github-deploy-site. That's your proof that it works, with no keys anywhere.
When it doesn't work (and it won't, the first time)
"Not authorized to perform sts:AssumeRoleWithWebIdentity." This is the one you'll meet. It means AWS read the token and the conditions in your trust policy didn't match. Check these, in order:
- The
subvalue. Does it match the repo and branch exactly? A workflow on a feature branch, a pull request or a tag has a differentsub. For deployments tied to a GitHub Environment, the format isrepo:<ORG>/<REPO>:environment:<NAME>. - The repo's age. GitHub has started adding immutable IDs into the
subclaim, in the formrepo:<ORG>@<ORG_ID>/<REPO>@<REPO_ID>:ref:refs/heads/<BRANCH>. This applies to repos created on or after 15 July 2026, and to older repos that opted in or were renamed since. If your repo emits the new form and your trust policy uses the old name-only form, assumption fails. Update the condition to match. - The audience. It should be
sts.amazonaws.com. - The workflow permissions. No
id-token: write, no token.
A handy trick: when you're stuck, temporarily print the claims your job is actually sending and compare them with your trust policy. Then remove that debugging step.
"The role name is GitHubActions and nothing works." Apparently naming the role exactly that has been reported to cause trouble. Pick something descriptive like github-deploy-site, which is better practice anyway.
Hardening, in order of effort
- Pin the branch in
sub. You already did. Resist the temptation to userepo:<ORG>/<REPO>:*"just to make it work." That allows any branch, any pull request and any environment in the repo. - One role per job, per environment. Production deploys and test deploys shouldn't share a role. Use a GitHub Environment (with protection rules, like required reviewers) for production and match it in the
sub. - Least privilege, always. Start with the three statements above and add actions only when something fails with an AccessDenied.
- Keep an eye on CloudTrail. With a clear
role-session-name, a weird deploy is easy to spot. - Delete the old access keys. After this works, remove those
AWS_ACCESS_KEY_IDsecrets and deactivate the old IAM user's keys. The whole point is that nothing is left lying around.
Quick checklist
- OIDC provider created in the AWS account
- Role with a trust policy locked to your repo and branch
- Permissions policy limited to what the pipeline touches
- Workflow has
id-token: write -
aws sts get-caller-identityshows the assumed role - Old access keys deleted
Wrapping up
That's the whole thing: one provider, one role, one workflow, zero stored secrets. Your pipeline gets credentials that expire on their own, and your security team gets to stop asking about that key.
If you try it and hit an error I didn't cover, send it my way. I like collecting the strange ones.
Next up: running AWS across a whole group of companies without losing track of who's paying for what.