Skip to content

3 min readDevang Pandya

GitHub Actions to AWS with OIDC, and no long-lived keys

Assume an IAM role from GitHub Actions, pin the subject claim including the 2026 immutable form, and retire the access key.

  • AWS
  • CI/CD
  • GitHub Actions

A long-lived access key in a CI system is a credential you cannot see use of, shared with every workflow that can read the secret, and rotated only after something has already gone wrong. OpenID Connect lets GitHub Actions assume an IAM role for the length of the job. There is no key to leak, and CloudTrail shows the assume-role call. The setup is short. The mistakes are in the trust policy.

What you are trusting

GitHub's issuer for Actions is https://token.actions.githubusercontent.com. You create that as an IAM OIDC identity provider once per account. The audience is sts.amazonaws.com. The subject claim is the part that decides which workflow may assume the role. IAM now requires that trust policies for this provider evaluate the subject (or job_workflow_ref). A role with no condition on the subject should not save, and you should not want it to.

The GitHub documentation for OIDC on AWS (opens in a new tab) is the page I send people to. Read the subject section, not just the screenshot of a trust policy.

Two subject formats, and you must match yours

Repositories created on or after 15 July 2026 use an immutable subject. So do repositories renamed or transferred after that date, and any older repository that has opted in. The claim carries the numeric owner id and repository id, so a recycled name cannot mint a token your role still trusts:

repo:octo-org@123456/octo-repo@789012:ref:refs/heads/main

Older repositories that have not opted in still emit the name-only form:

repo:octo-org/octo-repo:ref:refs/heads/main

If the trust policy has one form and the token has the other, AssumeRoleWithWebIdentity fails with a not-authorized error. It looks like a bad role ARN. It is a subject mismatch. Print the sub your repository actually emits and paste that. Do not guess. A GitHub environment changes the claim again: the subject contains the environment name instead of the git ref. If you protect production with an environment, the production role should trust that environment subject, not refs/heads/main.

I do not use a wildcard across every repository in the org for a role that can change production. A role per purpose, trusted by one repository and one ref or one environment, is the shape that survives a review.

The workflow

The job needs permission to write an OIDC token. It does not need a cloud secret.

permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@PINNED_SHA
        with:
          role-to-assume: arn:aws:iam::123456789012:role/github-deploy-prod
          aws-region: ap-south-1
      - run: aws sts get-caller-identity

Replace the region with the one you run in. Pin configure-aws-credentials to a full commit SHA. A floating major tag is a supply-chain decision, and a placeholder like PINNED_SHA will not work until you replace it. The same applies to actions/checkout.

A trust policy that matches the immutable form looks like this. Yours must match the claim your repository emits, including the ids.

{
  "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:octo-org@123456/octo-repo@789012:ref:refs/heads/main"
        }
      }
    }
  ]
}

What the role is allowed to do

The trust policy answers who. The permissions policy answers what, and it should be the one deploy. Push to one ECR repository. Register one task definition. Update one service. Pass one execution role, if ECS requires it, and nothing else. A role that can iam:* because "the pipeline might need it later" is the long-lived key, wearing a different hat.

Fork pull requests do not match a subject pinned to your repository, which is what you want. Do not switch the workflow to pull_request_target to "make OIDC work" on pull requests from forks. That runs untrusted code in the context of the base repository.

Retire the key on purpose

After the role works, leave the old key in place but unused, and watch CloudTrail. When the only AWS calls from CI are AssumeRoleWithWebIdentity into the new role, deactivate the key. A week later, if nothing has complained, delete it, including the copy in the old Jenkins credential store and the one in someone's password manager "just in case".

This site's own deploy does not use this pattern. It ships with a dedicated SSH key to a single host, because that is how the box is operated. For an application account, I would still start with OIDC.

The wider delivery path, including what happens after the image exists, is the DevOps and Kubernetes work. The trust policy above is the part I refuse to leave as an IAM user.

Want this looked at in your account?

Devang Pandya works through these problems with teams who already have something in production. Tell us what is in the way.