Skip to main content

CI/CD and automation · Intermediate · 20 min

Deploy to AWS from GitHub Actions with OIDC instead of access keys

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · AWS CLI 2.42.4 · GitHub Actions

You will replace the long-lived AWS access keys in your GitHub secrets with an IAM role that only one repository's production environment can assume, and deploy a static site to S3 with it.

On this page

Most first deploys from GitHub Actions to AWS use an IAM user's access key pasted into two repository secrets, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. That key never expires. Anyone who gets it, through a leaked log, a compromised action or a former collaborator's laptop, can use it from anywhere until somebody notices and deletes it.

OpenID Connect (OIDC) removes the stored key. Each workflow run asks GitHub for a signed token that says, among other things, which repository and environment the job runs in. AWS checks the signature, compares those claims with the role's trust policy, and hands back credentials that last about an hour. There is nothing in GitHub to steal, and a token from any other repository is refused.

In this tutorial you register GitHub as an identity provider in IAM, create a role that only your repository's production environment can assume, give it exactly the permissions aws s3 sync needs for one bucket, and write the workflow that uses it. Then you delete the old access key.

IAM roles and identity providers are free; a deploy of a small site makes only a few S3 requests. If your readers are in Africa, the Cape Town Region (af-south-1) cuts latency, but it is an opt-in Region you must enable first; the steps here work the same in any Region.

Commands run on your laptop unless they say otherwise. Policy files go in ~/oidc-deploy-demo.

Step 1 · Register GitHub as an identity provider in IAM

AWS needs to know which token issuer it may trust. GitHub's issuer is https://token.actions.githubusercontent.com, and the audience its official AWS action asks for is sts.amazonaws.com. An account holds one provider per URL, so first check whether someone has already added it:

Bash
mkdir -p ~/oidc-deploy-demo
cd ~/oidc-deploy-demo
aws iam list-open-id-connect-providers --query 'OpenIDConnectProviderList[].Arn' --output text

If the output contains oidc-provider/token.actions.githubusercontent.com, skip the next command and reuse that provider. Otherwise, create it:

Bash
aws iam create-open-id-connect-provider \
  --url https://token.actions.githubusercontent.com \
  --client-id-list sts.amazonaws.com \
  --query OpenIDConnectProviderArn --output text

There is no --thumbprint-list. It is optional: IAM checks GitHub's key endpoint against its own list of trusted certificate authorities, so you do not have to paste a certificate hash that goes stale when GitHub rotates certificates.

The provider on its own grants nothing. It only lets roles in this account name GitHub as a trusted issuer. Who may create one matters, though, so keep iam:CreateOpenIDConnectProvider for administrators.

Check it worked

The command prints an ARN such as arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com. This shows the provider's URL and audience:

Bash
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
PROVIDER_ARN="arn:aws:iam::${ACCOUNT_ID}:oidc-provider/token.actions.githubusercontent.com"
aws iam get-open-id-connect-provider --open-id-connect-provider-arn "$PROVIDER_ARN" \
  --query '[Url, ClientIDList]' --output text

You see token.actions.githubusercontent.com and sts.amazonaws.com.

Step 2 · Write a trust policy for one repository and one environment

The trust policy decides which GitHub workflows may become the role. Set two values first, replacing both with yours: your repository as owner/name, and the bucket that serves your site.

Bash
GITHUB_REPO=your-org/your-site
BUCKET=your-site-bucket

Now write the policy. The heredoc is unquoted so the shell fills in your account ID and repository:

Bash
cat > trust.json <<JSON
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "GitHubProductionDeploysOnly",
      "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:${GITHUB_REPO}:environment:production"
        }
      }
    }
  ]
}
JSON
python3 -m json.tool trust.json > /dev/null && echo "trust.json ok"

The two conditions do different jobs:

  • aud says the token was minted for AWS STS. On its own it proves almost nothing, because every GitHub workflow in the world that uses the official action asks for the same audience.
  • sub says where the token came from. When a job names an environment, GitHub sets it to repo:<owner>/<repo>:environment:<name>. StringEquals demands an exact match, so a job in another repository, a fork, or a job in your repository without environment: production is refused.

Why a wildcard on sub is dangerous

It is tempting to write "StringLike": { "...:sub": "repo:*" } or repo:your-org/* so one role serves many repositories. Think about who can produce a matching token. With repo:*, any GitHub user can create a repository, add a workflow, and assume your role: the only thing they need is your role's ARN, which is not a secret and often appears in public workflow files. IAM refuses a trust policy whose sub is missing or only *, but it accepts repo:*, so the check will not save you.

repo:your-org/* is safer but still lets every repository in the organisation, including a test repository a new joiner creates, deploy to production. repo:your-org/your-site:* accepts any branch and pull request workflow in the repository. Keep sub exact, and create a second role when a second repository needs access.

Check it worked

trust.json ok is printed, and the sub line names your real repository:

Bash
grep -F '"token.actions.githubusercontent.com:sub"' trust.json

The line ends with "repo:your-org/your-site:environment:production" with your names in place. If it shows repo::environment:production, GITHUB_REPO was empty when you wrote the file; set it and write the file again.

Step 3 · Create the role with only the deploy permissions

aws s3 sync site/ s3://bucket --delete needs three things: list the bucket to compare files, upload changed files, and delete files that are no longer in site/. Nothing else, not even reading objects back. s3:ListBucket acts on the bucket ARN; the object actions act on bucket/*:

Bash
cat > deploy-access.json <<JSON
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ListTheSiteBucket",
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::${BUCKET}"
    },
    {
      "Sid": "WriteAndPruneSiteFiles",
      "Effect": "Allow",
      "Action": ["s3:PutObject", "s3:DeleteObject"],
      "Resource": "arn:aws:s3:::${BUCKET}/*"
    }
  ]
}
JSON
python3 -m json.tool deploy-access.json > /dev/null && echo "deploy-access.json ok"

Create the role with the trust policy, cap each session at one hour, and add the permissions as an inline policy so they live and die with the role:

Bash
ROLE_ARN=$(aws iam create-role --role-name github-site-deploy \
  --description "GitHub Actions production deploys of ${GITHUB_REPO} to ${BUCKET}" \
  --assume-role-policy-document file://trust.json \
  --max-session-duration 3600 \
  --query Role.Arn --output text)
echo "$ROLE_ARN"
aws iam put-role-policy --role-name github-site-deploy \
  --policy-name deploy-site-bucket \
  --policy-document file://deploy-access.json

If your site sits behind CloudFront, the deploy usually also invalidates the cache. Add cloudfront:CreateInvalidation on your one distribution's ARN when you add that step to the workflow, not before.

Check it worked

echo prints an ARN such as arn:aws:iam::123456789012:role/github-site-deploy; keep it, the workflow needs it. These show what the role trusts and what it may do:

Bash
aws iam get-role --role-name github-site-deploy --query 'Role.AssumeRolePolicyDocument' --output json
aws iam list-role-policies --role-name github-site-deploy --query PolicyNames --output text

The first prints your trust policy with the provider ARN and the exact sub. The second prints deploy-site-bucket. aws iam list-attached-role-policies --role-name github-site-deploy shows an empty list: no managed policy such as AmazonS3FullAccess has crept in.

Step 4 · Set up the production environment in GitHub

The trust policy only accepts jobs that run in an environment called production, so create it and keep the deploy settings there. The role ARN, Region and bucket name are not secrets, so store them as environment variables, not secrets. Replace OWNER/REPO with your repository and the values with yours:

Bash
gh api --method PUT repos/OWNER/REPO/environments/production \
  -F 'deployment_branch_policy[protected_branches]=false' \
  -F 'deployment_branch_policy[custom_branch_policies]=true'
gh api --method POST repos/OWNER/REPO/environments/production/deployment-branch-policies -f name=main
gh variable set AWS_ROLE_ARN --env production --repo OWNER/REPO --body "arn:aws:iam::123456789012:role/github-site-deploy"
gh variable set AWS_REGION --env production --repo OWNER/REPO --body "eu-west-2"
gh variable set SITE_BUCKET --env production --repo OWNER/REPO --body "your-site-bucket"

The first two commands create the environment and allow deploys to it only from main. In the web interface the same rule sits under Settings > Environments > production > Deployment branches and tags > Selected branches and tags, with a rule for main. This matters more than it looks: the sub claim names the environment, not the branch, so without that rule a workflow pushed on any branch that says environment: production would get production credentials. Adding required reviewers makes each deploy wait for an approval; /tutorials/require-approval-before-production-deploys-with-github-environments walks through both settings.

Check it worked

gh variable list --env production --repo OWNER/REPO lists AWS_REGION, AWS_ROLE_ARN and SITE_BUCKET with your values. This lists the branches allowed to deploy:

Bash
gh api repos/OWNER/REPO/environments/production/deployment-branch-policies --jq '.branch_policies[].name'

It prints main and nothing else.

Step 5 · Write the deploy workflow

In your site's repository, create the workflow:

Bash
cd ~/path/to/your/site-repo
Bash
mkdir -p .github/workflows
cat > .github/workflows/deploy-site.yml <<'YAML'
name: Deploy site

on:
  push:
    branches: [main]

permissions:
  contents: read

concurrency:
  group: deploy-production
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v7

      - name: Get short-lived AWS credentials
        uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: ${{ vars.AWS_ROLE_ARN }}
          aws-region: ${{ vars.AWS_REGION }}

      - name: Show who the job is in AWS
        run: aws sts get-caller-identity --query Arn --output text

      - name: Upload the site
        env:
          SITE_BUCKET: ${{ vars.SITE_BUCKET }}
        run: aws s3 sync site/ "s3://${SITE_BUCKET}" --delete
YAML

What each part does:

  • id-token: write lets the job ask GitHub for an OIDC token. It is set on the deploy job only, so other jobs you add later cannot request one. Any permissions block resets the rest to none, which is why contents: read is repeated for the checkout.
  • environment: production puts the job in the environment, which makes the token's sub match the trust policy and applies the branch rule you set.
  • configure-aws-credentials exchanges the token for role credentials and exports them to later steps. There are no aws-access-key-id inputs anywhere. Version 6 is the current major release. For a production repository, consider pinning it to the full commit hash of a release, as GitHub's own example does, because a tag can be moved.
  • aws s3 sync --delete uploads changed files and removes files you deleted from site/. GitHub's Ubuntu runners come with the AWS CLI v2 installed.
  • concurrency queues a second deploy behind the first instead of running both at once.

Check the file with actionlint 1.7.12 (/tutorials/github-actions-first-workflow shows how to install it):

Bash
actionlint
echo "actionlint exit code: $?"

Check it worked

actionlint prints nothing and the exit code is 0. grep -n 'id-token' .github/workflows/deploy-site.yml shows the permission inside the deploy job, not at the top of the file.

Step 6 · Push and watch the first OIDC deploy

Commit and push to main, then follow the run:

Bash
git add .github/workflows/deploy-site.yml
git commit -m "Deploy the site to S3 with OIDC"
git push origin main
gh run watch

In the run log, the "Get short-lived AWS credentials" step succeeds without any stored key, and "Show who the job is in AWS" prints an ARN like arn:aws:sts::123456789012:assumed-role/github-site-deploy/GitHubActions. The assumed-role part is the proof: the job is the role, for this run only.

If the credentials step fails with Not authorized to perform sts:AssumeRoleWithWebIdentity, the token's claims did not match the trust policy. The usual causes are a typo in the owner or repository name (the match is case-sensitive), an environment named differently from production, or a missing id-token: write. A message about the token itself, such as being unable to get the ID token, means the permission is missing.

Check it worked

On your laptop, list the bucket:

Bash
aws s3 ls "s3://${BUCKET}/"

Your site's files are there with the time of the run, and anything you had removed from site/ is gone.

Step 7 · Delete the old access keys

Once a deploy has gone through with OIDC, the old key is only a risk. Remove it from GitHub first, so no workflow can use it. Secrets can sit at repository level or in an environment, so check both (replace OWNER/REPO):

Bash
gh secret list --repo OWNER/REPO
gh secret list --env production --repo OWNER/REPO
gh secret delete AWS_ACCESS_KEY_ID --repo OWNER/REPO
gh secret delete AWS_SECRET_ACCESS_KEY --repo OWNER/REPO

Add --env production to the delete commands for secrets that were listed under the environment. Then find the IAM user the key belonged to. Replace github-actions-deploy with that user's name:

Bash
OLD_USER=github-actions-deploy
aws iam list-access-keys --user-name "$OLD_USER" \
  --query 'AccessKeyMetadata[].[AccessKeyId,Status,CreateDate]' --output text

Copy the key ID from the output, the value that starts with AKIA, and set it (replace the example):

Bash
KEY_ID=AKIAIOSFODNN7EXAMPLE

Deactivate the key before deleting it. An inactive key can be switched back on if something you forgot about breaks; a deleted one cannot:

Bash
aws iam update-access-key --user-name "$OLD_USER" --access-key-id "$KEY_ID" --status Inactive
aws iam list-access-keys --user-name "$OLD_USER" --query 'AccessKeyMetadata[].[AccessKeyId,Status]' --output text

The key now shows Inactive. Push a small change to main and watch the deploy still pass. Over the next day or two, check that nothing else has tried the key; the date in the output stops changing once nothing uses it:

Bash
aws iam get-access-key-last-used --access-key-id "$KEY_ID" --query 'AccessKeyLastUsed.[LastUsedDate,ServiceName]' --output text

If something breaks, switch the key back on with --status Active and fix that caller first. A day or two later, with nothing broken, delete the key:

Bash
aws iam delete-access-key --user-name "$OLD_USER" --access-key-id "$KEY_ID"

If that user existed only for CI, delete it too. These two commands show its policies:

Bash
aws iam list-attached-user-policies --user-name "$OLD_USER" --query 'AttachedPolicies[].PolicyArn' --output text
aws iam list-user-policies --user-name "$OLD_USER" --query PolicyNames --output text

Detach or delete any they list (aws iam detach-user-policy for the first, aws iam delete-user-policy for the second). When both print nothing, delete the user:

Bash
aws iam delete-user --user-name "$OLD_USER"

delete-user fails with DeleteConflict while the user still has keys, policies, group memberships or a console password, and the message says which.

Check it worked

gh secret list no longer shows AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY. aws iam get-user --user-name "$OLD_USER" fails with NoSuchEntity. Push another small change to main: the deploy still works, because it never used the key.

Step 8 · Clean up

If you followed along only to learn, remove what you created. If you keep the deploy, keep the workflow, the role and the provider, and stop here. Otherwise, delete the workflow first so no run tries the role, from your site's repository:

Bash
git rm .github/workflows/deploy-site.yml
git commit -m "Remove OIDC deploy"
git push origin main

Deleting the role makes any workflow that still refers to it fail at the credentials step. The first command lists every role in the account that trusts GitHub; note what it prints before you delete anything:

Bash
cd ~/oidc-deploy-demo
aws iam list-roles --query "Roles[?contains(to_string(AssumeRolePolicyDocument), 'token.actions.githubusercontent.com')].RoleName" --output text
aws iam delete-role-policy --role-name github-site-deploy --policy-name deploy-site-bucket
aws iam delete-role --role-name github-site-deploy

If the first command printed only github-site-deploy, or nothing, no other role needs the identity provider, so delete it. If it listed other roles, skip this command: deleting the provider breaks their workflows.

Bash
aws iam delete-open-id-connect-provider --open-id-connect-provider-arn "$PROVIDER_ARN"

Remove the policy files:

Bash
cd ~
rm -rf ~/oidc-deploy-demo

In GitHub, gh api --method DELETE repos/OWNER/REPO/environments/production removes the environment and its variables. Your bucket and site stay as they are.

Check it worked

aws iam get-role --role-name github-site-deploy fails with NoSuchEntity, and aws iam list-open-id-connect-providers no longer lists GitHub's provider.

Next steps

  • Make every production deploy wait for a reviewer: /tutorials/require-approval-before-production-deploys-with-github-environments.
  • Apply the same least-privilege thinking to roles for apps on EC2: /tutorials/create-an-iam-role-with-a-least-privilege-policy.
  • Put CloudFront in front of the bucket and invalidate it after each deploy: /tutorials/host-a-static-site-on-s3-and-cloudfront.
  • Do the same keyless set-up for Google Cloud: /tutorials/authenticate-github-actions-to-google-cloud-with-workload-identity.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.