Nov 23, 20265 min read

Deploy to AWS from GitHub Actions with OIDC

How to replace long-lived AWS access keys in GitHub secrets with an OIDC identity provider and a short-lived role, including the trust policy conditions that decide which repositories and branches can assume it.

Illustration for Deploy to AWS from GitHub Actions with OIDC

Putting an AWS access key in GitHub secrets works until someone has to rotate it, or until it leaks through a fork, a log line or a compromised action. OpenID Connect replaces the stored key with a token that GitHub signs for each run and AWS exchanges for credentials that expire in an hour.

The setup is three pieces: an identity provider, a role with a careful trust policy, and a few lines in the workflow.

What do you need to configure?

PieceValueWhy it matters
Identity providerhttps://token.actions.githubusercontent.com, audience sts.amazonaws.comLets AWS verify GitHub's tokens
Trust policyConditions on aud and subDecides which repo and branch may assume the role
Permissions policyOnly what the deploy needsLimits damage from a compromised workflow
Workflow permissionsid-token: writeLets the job request the token
Credentials actionrole-to-assumeExchanges the token for credentials

How do you create the provider and role?

GitHub documents the OIDC setup for AWS, and the AWS side is a standard IAM OIDC provider. In Terraform:

resource "aws_iam_openid_connect_provider" "github" { url = "https://token.actions.githubusercontent.com" client_id_list = ["sts.amazonaws.com"] } data "aws_iam_policy_document" "trust" { statement { 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 = "StringLike" variable = "token.actions.githubusercontent.com:sub" values = ["repo:my-org/my-repo:ref:refs/heads/main"] } } } resource "aws_iam_role" "deploy" { name = "github-deploy" assume_role_policy = data.aws_iam_policy_document.trust.json }

Attach a permissions policy to github-deploy that covers only the deploy, for example pushing to one ECR repository and updating one ECS service.

Why must the trust policy check the subject?

The aud condition alone only proves the token came from GitHub, and GitHub issues tokens for every repository. Without the sub condition, a workflow in any repository on GitHub that knows the role ARN can assume the role. The subject has the form repo:ORG/REPO:ref:refs/heads/BRANCH, and it changes with the trigger:

  • A job that uses a GitHub environment has repo:ORG/REPO:environment:production.
  • A pull request run has repo:ORG/REPO:pull_request.
  • A tag build has repo:ORG/REPO:ref:refs/tags/v1.2.3.

Use StringLike with a pattern for several branches, and keep pull request runs on a separate role that can only plan and read.

What does the workflow look like?

name: deploy on: push: branches: [main] permissions: id-token: write # lets the job request the OIDC token contents: read jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - uses: aws-actions/configure-aws-credentials@v6 with: role-to-assume: arn:aws:iam::123456789012:role/github-deploy aws-region: eu-central-1 - run: aws sts get-caller-identity

The configure-aws-credentials action requests the token, calls STS and exports the temporary credentials for the later steps. Without id-token: write, the job fails before it reaches AWS.

How do you debug an AssumeRoleWithWebIdentity error?

Print the claims in a throwaway job to see what GitHub sent, and compare them with the trust policy character by character. The common mistakes are a branch pattern that does not match the trigger, a pull request job against a role that only allows main, an environment set on the job that changes the subject, and a role ARN that points to the wrong account. CloudTrail records the failed AssumeRoleWithWebIdentity call with the reason.

How does Encore avoid keys in GitHub?

Encore

Encore brings infrastructure from code, and its deploy path has no AWS credentials in GitHub at all: per the own cloud docs, you create an IAM role in your account that the Encore platform assumes, with the external ID check turned on, and link your repository through the GitHub integration. A push to the configured branch builds the app and deploys it to your AWS account.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.
Encore provisions the infrastructure your application needs in your own AWS or GCP account.
Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create shop-api --example=ts/hello-world$ cd shop-api$ git push encore
// uploads/uploads.ts import { Bucket } from "encore.dev/storage/objects"; import { SQLDatabase } from "encore.dev/storage/sqldb"; // Declared in code. The deploy provisions the S3 bucket and the database // in the connected AWS account, so the pipeline needs no extra permissions for them. export const uploads = new Bucket("uploads", { versioned: false }); export const db = new SQLDatabase("uploads", { migrations: "./migrations" });

The code is deployed to the connected account by the platform, and the buckets and databases it declares are created there as the AWS infrastructure docs describe, so there is no deploy role in your workflow to scope. Encore also builds the app and runs its tests on pull requests, so your workflow can shrink to checks that are specific to your repository.

A coding agent that opens a pull request with a new bucket or database does not need cloud credentials in its environment, because the declared resources are created by the platform during the deploy.

Best fit: Teams that want deploys to their own AWS account without managing a deploy role and its trust policy in CI.

Frequently asked questions

How does GitHub Actions authenticate to AWS with OIDC?

The workflow requests a signed ID token from GitHub, and AWS exchanges it for temporary credentials through sts:AssumeRoleWithWebIdentity. The role's trust policy checks the token's audience and subject claims, so no access key is stored in GitHub.

What should the trust policy condition on?

Always condition on the token's sub claim, for example repo:my-org/my-repo:ref:refs/heads/main, together with the aud claim set to sts.amazonaws.com. A role with no sub condition can be assumed by workflows in any GitHub repository.

Why do I get Not authorized to perform sts:AssumeRoleWithWebIdentity?

The token's claims did not match the trust policy. The usual causes are a sub value that differs from the pattern, such as a pull request run or an environment-based job, a missing id-token write permission in the workflow, or an audience mismatch with the provider.

How does Encore deploy to AWS without keys in GitHub?

With Encore, you create an IAM role in your own AWS account that the Encore platform assumes, with an external ID required, and link the GitHub repository. The role and its permissions stay in your account, and deleting it ends access. A push to the configured branch triggers the deploy, so the workflow holds no AWS credentials.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders