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.

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.
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.
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:
repo:ORG/REPO:environment:production.repo:ORG/REPO:pull_request.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.
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.
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.
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.

Run in your terminal to get started locally.
// 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.
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.
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.
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.
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.