AWS Builder Center
Deploy from GitHub Actions without AWS access keys: an OIDC role in CDK

Deploy from GitHub Actions without AWS access keys: an OIDC role in CDK

Create the GitHub OIDC provider and a deploy role in CDK, and scope the trust policy to one repo and branch so no long-lived AWS keys sit in GitHub secrets.

Estudiante de ingeniería de software | DevOps
A lot of GitHub deploy pipelines still run on an IAM user's access key stored in repository secrets. The key never expires, and anyone who can edit a workflow in that repository can use it. GitHub Actions can instead present a short-lived OIDC token, and AWS can exchange it for temporary credentials. The AWS side is one identity provider and one role, which is a short CDK stack.

What AWS checks

GitHub signs a JWT for each workflow run. The identity provider in IAM uses  https://token.actions.githubusercontent.com  as its URL, and when you use the official  aws-actions/configure-aws-credentials  action the audience is  sts.amazonaws.com . The workflow only needs the  id-token: write  permission to request the token.
The trust policy on the role is where the security actually sits. GitHub's documentation says you must define at least one condition, because otherwise repositories you do not control could request credentials for your role. In practice that means checking both the  aud  claim and the  sub  claim, and keeping  sub  as narrow as the pipeline allows.

The CDK stack

This is a plain JavaScript CDK app, so it runs with  cdk synth  and no TypeScript build step. It uses  OidcProviderNative , which wraps the native  AWS::IAM::OIDCProvider  resource. The CDK documentation points new users there instead of  OpenIdConnectProvider , which stays in the library mainly because other constructs depend on it.
const cdk = require('aws-cdk-lib');
const iam = require('aws-cdk-lib/aws-iam');

class GithubDeployRoleStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);

const provider = new iam.OidcProviderNative(this, 'GithubProvider', {
url: 'https://token.actions.githubusercontent.com',
clientIds: ['sts.amazonaws.com'],
});

const principal = new iam.WebIdentityPrincipal(provider.oidcProviderArn, {
StringEquals: {
'token.actions.githubusercontent.com:aud': 'sts.amazonaws.com',
'token.actions.githubusercontent.com:sub': 'repo:my-org/my-repo:ref:refs/heads/main',
},
});

new iam.Role(this, 'DeployRole', {
assumedBy: principal,
maxSessionDuration: cdk.Duration.hours(1),
});
}
}

const app = new cdk.App();
new GithubDeployRoleStack(app, 'GithubDeployRole');
No thumbprint is passed. The construct documentation says that when  thumbprints  is omitted, IAM retrieves and uses the thumbprint of the top intermediate CA, and the IAM documentation says AWS validates the provider against its own library of trusted root CAs, falling back to thumbprints only when that is not possible.
Synthesizing the stack with aws-cdk-lib 2.275.0 produces this (trimmed to the two resources that matter):
Resources:
GithubProvider1CDE27EB:
Type: AWS::IAM::OIDCProvider
Properties:
ClientIdList:
- sts.amazonaws.com
Url: https://token.actions.githubusercontent.com
DeployRole885297C3:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
token.actions.githubusercontent.com:aud: sts.amazonaws.com
token.actions.githubusercontent.com:sub: repo:my-org/my-repo:ref:refs/heads/main
Effect: Allow
Principal:
Federated:
Ref: GithubProvider1CDE27EB
The role's principal is the provider created in the same template, the action is  sts:AssumeRoleWithWebIdentity , and both conditions are present. The role has no permissions yet. Attach a policy that covers only what the pipeline needs to deploy.

Choosing the sub condition

The  sub  claim identifies where the run came from. GitHub documents these shapes:
  • A branch:  repo:octo-org/octo-repo:ref:refs/heads/octo-branch 
  • An environment:  repo:octo-org/octo-repo:environment:prod 
A wildcard such as  repo:octo-org/octo-repo:*  with  StringLike  matches every ref in the repository, pull request merge branches included. That is convenient and also much broader than a deploy role should be. I would pin production roles to a branch or, better, to a GitHub environment, since environments can carry their own protection rules.
One more detail from the same documentation: for repositories created after July 15, 2026, or ones that opted in, the subject can include numeric IDs, for example  repo:octo-org@123456/octo-repo@456789:ref:refs/heads/octo-branch . Before copying a condition, check the claim your repository actually emits.

The workflow side

name: deploy
on:
push:
branches: [main]

permissions:
id-token: write
contents: read

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@<pinned version>
with:
role-to-assume: arn:aws:iam::111122223333:role/<deploy role name>
aws-region: us-east-1
- run: aws sts get-caller-identity
The final step is only there to show which identity the job ended up with. The action version and the account ID are placeholders. Pin the action to a released version or a commit SHA, the way GitHub's own example does.

Two things that can surprise you

An identity provider URL must be unique within an account. If the account already has a GitHub provider, created by hand or by another stack, a second one for the same URL cannot be created, so reuse the existing provider instead of declaring it again.
The default removal policy for the provider construct is  DESTROY . If several roles or stacks end up depending on one provider, deleting the stack that owns it removes the provider for all of them. A provider shared across projects is better kept in its own small stack.
The workflow in this article was not run against a real AWS account. The CDK output above comes from an actual  cdk synth , and the identity provider and trust-policy details come from the GitHub and AWS documentation.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article