DEV Community

Cover image for GitHub Actions to AWS with OIDC: deploying without a single access key
Abraiz Bashir
Abraiz Bashir

Posted on Originally published at builder.aws.com

GitHub Actions to AWS with OIDC: deploying without a single access key

The classic way to deploy to AWS from CI goes like this: create an IAM user, generate an access key, paste it into your CI secrets, and hope it never leaks. Those keys don't expire. If one ends up in a log, a fork or an old laptop backup, someone has long-term access to your account until you notice.

GitHub Actions doesn't need them. With OIDC, every workflow run asks AWS for credentials that last one hour, and AWS checks exactly which repo and branch is asking before it hands anything over.

This is the first post in a series where I build one small project and grow its pipeline one step at a time. In this one I set up OIDC from scratch with Terraform, deploy a real app with it, and walk through the error that broke my very first run. Spoiler: GitHub now puts numeric IDs in the token, and my trust policy wasn't expecting them.

What I built

DeployLog is a tiny API that records deployments:

  • POST /deployments saves one: service, version, commit, environment and who deployed it
  • GET /deployments?service=deploylog lists the latest 20

The twist: the pipeline that deploys DeployLog writes its own record to it after every successful deploy. So every post in this series ends with real data you can query.

The stack is Lambda (Python 3.13), API Gateway HTTP API and DynamoDB, deployed with Terraform from GitHub Actions. The repo has four parts:

Path What it is
app/ The Lambda handler
bootstrap/ One-time setup you apply yourself: OIDC provider, deploy role, state bucket
infra/ The app's infrastructure, applied only by CI
.github/workflows/deploy.yml The pipeline

All the code is on GitHub at MuhammadAbraiz/deploylog-aws. The tag step-01 matches this post.

How OIDC works between GitHub and AWS

Four things happen at the start of every run:

  1. The job has id-token: write permission, so it can ask GitHub for a signed token (a JWT). The token says which repo, branch and workflow it came from.
  2. The configure-aws-credentials action sends that token to AWS STS with AssumeRoleWithWebIdentity.
  3. STS checks the signature against the GitHub OIDC provider registered in IAM. Then it checks the role's trust policy: does the token's audience (aud) and subject (sub) match the conditions?
  4. If both match, STS returns temporary credentials. Mine expire after one hour.

The only thing GitHub stores is the role ARN. That isn't a credential on its own, because only a token from this exact repo and branch can use it.

The sub claim does most of the work. It's a string like repo:OWNER/REPO:ref:refs/heads/main, and your trust policy should match it exactly. Keep that format in mind, because it's where my first run went wrong.

Step 1: the one-time bootstrap

The pipeline can't create the role it uses to log in, so a few things have to exist first. I keep them in a separate bootstrap/ folder that I apply once from my own machine with admin credentials:

  • the GitHub OIDC provider in IAM
  • the deploy role and its trust policy
  • a permission boundary for any Lambda role the pipeline creates
  • an S3 bucket and DynamoDB table for the app's Terraform state

The OIDC provider. An AWS account can only have one provider per URL. The account I used already had one for GitHub, so instead of creating a duplicate I read it with a data source, behind a flag:

resource "aws_iam_openid_connect_provider" "github" {
  count          = var.create_oidc_provider ? 1 : 0
  url            = "https://token.actions.githubusercontent.com"
  client_id_list = ["sts.amazonaws.com"]
}

data "aws_iam_openid_connect_provider" "github" {
  count = var.create_oidc_provider ? 0 : 1
  url   = "https://token.actions.githubusercontent.com"
}
Enter fullscreen mode Exit fullscreen mode

The GitHub OIDC provider in IAM

The trust policy. This is the version I started with. It checks the audience and only allows the main branch of one repo:

data "aws_iam_policy_document" "trust" {
  statement {
    actions = ["sts:AssumeRoleWithWebIdentity"]

    principals {
      type        = "Federated"
      identifiers = [local.oidc_provider_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"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The permission boundary. The deploy role has to create an IAM role for the Lambda function. That's a classic privilege escalation path: a role that can create roles can create one with admin rights. So the deploy role may only create or change roles named deploylog-lambda-*, and only if they carry this boundary:

statement {
  sid = "LambdaRoleWithBoundary"
  actions = [
    "iam:CreateRole", "iam:PutRolePolicy", "iam:DeleteRolePolicy",
    "iam:AttachRolePolicy", "iam:DetachRolePolicy",
  ]
  resources = [local.lambda_role]

  condition {
    test     = "StringEquals"
    variable = "iam:PermissionsBoundary"
    values   = [aws_iam_policy.lambda_boundary.arn]
  }
}
Enter fullscreen mode Exit fullscreen mode

The boundary itself only allows PutItem and Query on the app's table, plus writing logs. Even if someone misused the pipeline, any Lambda role it creates can't do more than that.

One terraform apply created 8 resources. The outputs go into the GitHub repo settings: the role ARN and the state bucket name as secrets, and the lock table name as a variable. I keep the role ARN as a secret anyway, so the account ID never shows up in public logs.

Repository secrets: a role ARN and a bucket name, no AWS keys

Step 2: the workflow

The workflow runs on every push to main. The part that matters for OIDC is small:

permissions:
  id-token: write   # lets the job request an OIDC token
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - uses: hashicorp/setup-terraform@v4
        with:
          terraform_version: 1.14.8
          terraform_wrapper: false

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
          role-session-name: deploylog-${{ github.run_id }}
          aws-region: us-east-1
          mask-aws-account-id: true
Enter fullscreen mode Exit fullscreen mode

mask-aws-account-id hides the account ID in the logs. My repo is public, so that matters.

After that come terraform init, plan and apply in the infra/ folder. The last step records the deployment. The POST route uses IAM auth, so only signed requests get in. curl can sign the request itself with the session credentials the action just exported:

api_url="$(terraform output -raw api_url)"
payload=$(printf '{"service":"deploylog","version":"%s","commit":"%s","environment":"prod","deployed_by":"%s"}' \
  "${GITHUB_SHA::7}" "$GITHUB_SHA" "$GITHUB_ACTOR")

curl --fail-with-body -sS -X POST "$api_url/deployments" \
  --aws-sigv4 "aws:amz:${AWS_REGION}:execute-api" \
  --user "${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}" \
  -H "x-amz-security-token: ${AWS_SESSION_TOKEN}" \
  -H "content-type: application/json" \
  -d "$payload"
Enter fullscreen mode Exit fullscreen mode

I checked the other side too: an unsigned POST to the same URL gets 403 Forbidden.

Step 3: the first push, and AccessDenied

I pushed to main, and the first run failed at the credentials step, before Terraform even started:

Could not assume role with OIDC: Not authorized to perform sts:AssumeRoleWithWebIdentity
Enter fullscreen mode Exit fullscreen mode

Run 1 failed at the credentials step

This error tells you almost nothing. It means "the token didn't match the trust policy", but not which part. I went through the usual suspects:

  • Audience: the OIDC provider's client ID list had sts.amazonaws.com. Fine.
  • Trust policy: aud and sub conditions were there, repo and branch spelled correctly. Looked fine.
  • Permissions: id-token: write was set in the workflow. Fine.

Everything looked right, so I stopped guessing and asked AWS what it actually received. Every failed AssumeRoleWithWebIdentity call is logged in CloudTrail, including the token's subject:

aws cloudtrail lookup-events \
  --lookup-attributes AttributeKey=EventName,AttributeValue=AssumeRoleWithWebIdentity \
  --max-results 5
Enter fullscreen mode Exit fullscreen mode

The userName field of the event is the exact sub claim GitHub sent. It wasn't what my policy expected:

Expected: repo:MuhammadAbraiz/deploylog-aws:ref:refs/heads/main
Received: repo:MuhammadAbraiz@98274826/deploylog-aws@1471642139:ref:refs/heads/main
Enter fullscreen mode Exit fullscreen mode

GitHub had added a numeric ID after the owner and after the repo name.

The fix: GitHub's immutable sub claim

This isn't a bug. GitHub changed the default sub format in 2026. According to GitHub's OIDC reference, repositories created after 15 July 2026 use a format that includes the owner ID and the repository ID. Older repos keep the old format unless you opt in at the organization or repository level. My repo was brand new, so it got the new one.

The reason is a real attack. Names can be reused: if someone deletes or renames a repo, another account could create a repo with the old name and get a token with the same sub. A trust policy that only checks names would happily trust it. The numeric IDs are assigned once and never reused, so a policy that checks them stays tied to the original repo.

To fix it, you need two numbers. Both are public:

curl -s https://api.github.com/repos/MuhammadAbraiz/deploylog-aws \
  | jq '{owner_id: .owner.id, repo_id: .id}'
Enter fullscreen mode Exit fullscreen mode

Then I built the sub from the names and IDs:

variable "github_owner_id" { default = "98274826" }
variable "github_repo_id"  { default = "1471642139" }

locals {
  repo_owner = split("/", var.github_repo)[0]
  repo_name  = split("/", var.github_repo)[1]
  github_sub = "repo:${local.repo_owner}@${var.github_owner_id}/${local.repo_name}@${var.github_repo_id}:ref:refs/heads/main"
}
Enter fullscreen mode Exit fullscreen mode

The condition now uses values = [local.github_sub]. terraform plan showed exactly one change, the sub value in the trust policy:

~ "token.actions.githubusercontent.com:sub" =
    "repo:MuhammadAbraiz/deploylog-aws:ref:refs/heads/main"
 -> "repo:MuhammadAbraiz@98274826/deploylog-aws@1471642139:ref:refs/heads/main"

Plan: 0 to add, 1 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

The fixed trust policy with numeric IDs

If you have existing OIDC roles, check which format each repo sends before you rename or transfer anything. Per the same GitHub docs, renames and transfers after 15 July 2026 also move to the new format, which would break a name-only trust policy in exactly the way it broke mine.

It works

I applied the bootstrap change, pushed again, and run 2 went green in 1 minute 11 seconds. While I was in there I also bumped both actions to their Node 24 versions (configure-aws-credentials@v6 and setup-terraform@v4), because run 1 had warned that Node 20 is deprecated.

Run 1 denied, run 2 deployed

The credentials step now shows the job trading its GitHub token for an AWS session:

Assuming role with OIDC

Terraform created the table, the Lambda function and the API. Then the last step recorded the deployment:

The pipeline records its own deployment

And the API has the record the pipeline wrote about itself:

GET /deployments returns the record

The pipeline never stores or relies on long-lived AWS access keys. Admin credentials were used once, from my own machine, for the bootstrap. The only long-lived thing in GitHub is a role ARN that nothing outside main of this one repo can use.

Checklist

If you're setting this up yourself, these are the things I'd check:

  • Match both aud and sub with StringEquals. A trust policy that only checks the audience trusts every GitHub repo in the world.
  • Pin the branch (or a GitHub environment) in the sub, not just the repo.
  • Check which sub format your repo sends. New repos send the IDs. Older repos switch when renamed or transferred.
  • When you get AccessDenied, read CloudTrail. The userName of the AssumeRoleWithWebIdentity event is the exact sub GitHub sent.
  • Reuse the account's OIDC provider. There can only be one per URL.
  • Put a permission boundary on any role your pipeline can create. Otherwise the deploy role is one step away from admin.
  • Mask the account ID with mask-aws-account-id: true if your logs are public.

What's next

The pipeline still locks Terraform state with a DynamoDB table, the classic setup. Terraform can now lock state in S3 directly, so a later post in this series removes that table. Next week, though, is re:Invent, and I'll write about what's new for people who maintain pipelines.

The full code is in deploylog-aws on GitHub, tag step-01. If you hit a different OIDC error, leave a comment with what CloudTrail showed and I'll take a look.

Sources


Originally published on AWS Builder Center.

Top comments (0)