CDK constructs to use OpenID Connect for authenticating your Github Action workflow with AWS IAM
Project description
[!IMPORTANT] Migrating to
v4? See Migration Guide at the end of this README.
AWS CDK Github OpenID Connect
AWS CDK constructs that define:
- Github Actions as OpenID Connect Identity Provider into AWS IAM
- IAM Roles that can be assumed by Github Actions workflows
These constructs allows you to harden your AWS deployment security by removing the need to create long-term access keys for Github Actions and instead use OpenID Connect to Authenticate your Github Action workflow with AWS IAM.
Background information
- GitHub Actions: Secure cloud deployments with OpenID Connect on Github Changelog Blog.
- Security hardening your deployments on Github Docs.
- Assuming a role with
aws-actions/configure-aws-credentials. - Shout-out to Richard H. Boyd for helping me to debug Github OIDC setup with AWS IAM and his Deploying to AWS with Github Actions-talk.
- Shout-out to Aidan W Steele and his blog post AWS federation comes to GitHub Actions for being the original inspiration for this.
Getting started
pnpm add -D aws-cdk-github-oidc
OpenID Connect Identity Provider trust for AWS IAM
To create a new Github OIDC provider configuration into AWS IAM:
import { GithubActionsIdentityProvider } from "aws-cdk-github-oidc";
const provider = new GithubActionsIdentityProvider(scope, "GithubProvider");
In the background this creates an OIDC provider trust configuration into AWS IAM with an issuer URL of https://token.actions.githubusercontent.com and audiences (client IDs) configured as ['sts.amazonaws.com'] (which matches the aws-actions/configure-aws-credentials implementation).
Retrieving a reference to an existing Github OIDC provider configuration
Remember, there can be only one (Github OIDC provider per AWS Account), so to retrieve a reference to existing Github OIDC provider use fromAccount static method:
import { GithubActionsIdentityProvider } from "aws-cdk-github-oidc";
const provider = GithubActionsIdentityProvider.fromAccount(
scope,
"GithubProvider"
);
Defining a role for Github Actions workflow to assume
import { GithubActionsRole } from "aws-cdk-github-oidc";
const uploadRole = new GithubActionsRole(scope, "UploadRole", {
provider: provider, // reference into the OIDC provider
owner: "octo-org", // your repository owner (organization or user) name
repo: "octo-repo", // your repository name (without the owner name)
filter: "ref:refs/tags/v*", // JWT sub suffix filter, defaults to '*'
});
// use it like any other role, for example grant S3 bucket write access:
myBucket.grantWrite(uploadRole);
You may pass in any iam.RoleProps into the construct's props, except assumedBy which will be defined by this construct (CDK will fail if you do):
const deployRole = new GithubActionsRole(scope, "DeployRole", {
provider: provider,
owner: "octo-org",
repo: "octo-repo",
roleName: "MyDeployRole",
description: "This role deploys stuff to AWS",
maxSessionDuration: cdk.Duration.hours(2),
});
// You may also use various "add*" policy methods!
// "AdministratorAccess" not really a good idea, just for an example here:
deployRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName("AdministratorAccess")
);
Subject Filter
By default the value of filter property will be '*' which means any workflow (from given repository) from any branch, tag, environment or pull request can assume this role. To further stricten the OIDC trust policy on the role, you may adjust the subject filter as seen on the examples in Github Docs; For example:
filter value |
Descrition |
|---|---|
'ref:refs/tags/v*' |
Allow only tags with prefix of v |
'ref:refs/heads/demo-branch' |
Allow only from branch demo-branch |
'pull_request' |
Allow only from pull request |
'environment:Production' |
Allow only from Production environment |
Github Actions Workflow
To actually utilize this in your Github Actions workflow, use aws-actions/configure-aws-credentials to assume a role.
jobs:
whoami:
name: Who Am I
runs-on: ubuntu-latest
permissions:
id-token: write # needed to interact with GitHub's OIDC Token endpoint.
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6.1.1
with:
role-to-assume: arn:aws:iam::123456789012:role/MyUploadRole
#role-session-name: MySessionName # Optional
aws-region: us-east-1
- name: Get Caller Identity
run: |
aws sts get-caller-identity
Migration Guide
v2→v3
-
Install AWS CDK version
v2.237.0or newer required (due to support of OIDC provider removal policy):pnpm add -D aws-cdk-lib@^2.237.0
-
Install
v3.1.0(or newer v3 release) of this library:pnpm add -D aws-cdk-github-oidc@^3.1
-
No additional steps required, as the v3 major version does not introduce any breaking changes (just a lot of internal tooling changes).
v3→v4
[!CAUTION] The following steps describe a no-downtime migration path. It is the recommended approach, but somewhat involved: Hence some users may decide to use "destroy + redeploy" strategy instead, which causes downtime to authenticating from GitHub Actions to AWS using OIDC.
-
Ensure you are running
v3.1.0(or newer v3 release) of this library, see v2→v3. -
Configure
RETAINremoval policy for the provider:const provider = new GithubActionsIdentityProvider(this, "GithubProvider", { + removalPolicy: cdk.RemovalPolicy.RETAIN, }); -
Run
pnpm exec cdk diff, which will show an output similar to:Resources [~] Custom::AWSCDKOpenIdConnectProvider GithubProvider/Resource GithubProvider1CDE27EB ├─ [~] DeletionPolicy │ ├─ [-] Delete │ └─ [+] Retain └─ [~] UpdateReplacePolicy ├─ [-] Delete └─ [+] Retain
-
Deploy the changes
pnpm exec cdk deploy -
Once the
RETAINremoval policy has been successfully deployed, upgrade this library tov4.2(or newer v4 release):pnpm add -D aws-cdk-github-oidc@^4.2
-
Temporarily change from provider initializion to provider lookup:
- const provider = new GithubActionsIdentityProvider(this, "GithubProvider", { - removalPolicy: cdk.RemovalPolicy.RETAIN, - }); + const provider = GithubActionsIdentityProvider.fromAccount(this, "GithubProviderReference"); // NOTICE the different construct ID
⚠️ Notice the different construct ID (in the example
GithubProviderReferenceinstead of). This is required so that the CDK treats the GitHub OIDC provider lookup as a different "thing" and does not try to change the type of existing construct.GithubProvider -
Check
pnpm exec cdk diffwhich should look similar to:Resources [-] Custom::AWSCDKOpenIdConnectProvider GithubProvider/Resource GithubProvider1CDE27EB orphan [-] AWS::IAM::Role Custom::AWSCDKOpenIdConnectProviderCustomResourceProvider/Role CustomAWSCDKOpenIdConnectProviderCustomResourceProviderRole517FED65 destroy [-] AWS::Lambda::Function Custom::AWSCDKOpenIdConnectProviderCustomResourceProvider/Handler CustomAWSCDKOpenIdConnectProviderCustomResourceProviderHandlerF2C543E0 destroy
-
Deploy the changes with
pnpm exec cdk deploy -
Once the deployment has succeeded, remove the provider lookup and replace it with the original provider initialization:
- const provider = GithubActionsIdentityProvider.fromAccount(this, "GithubProviderReference"); + const provider = new GithubActionsIdentityProvider(this, "GithubProvider", { + removalPolicy: cdk.RemovalPolicy.RETAIN, + });
-
Copy the ARN of the existing OIDC provider, it will be in the format of:
arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com # REPLACE with your account ID -
Use cdk import:
pnpm exec cdk import <YOUR_STACK_NAME>
... and when asked, input the provider ARN you copied in step 10:
<YOUR_STACK_NAME>/GithubProvider/Resource (AWS::IAM::OIDCProvider): enter Arn (empty to skip)
-
You should be done now, but you may want to perform manual verification in addition to drift detection and/or `cdk diff`` to verify.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aws_cdk_github_oidc-4.2.5.tar.gz.
File metadata
- Download URL: aws_cdk_github_oidc-4.2.5.tar.gz
- Upload date:
- Size: 122.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abea4e53dc91edbccb64042917481fa1c2226981817ce24efa5a2309a12d9b06
|
|
| MD5 |
91068a1f4dbb529b6cccca1d8a88798a
|
|
| BLAKE2b-256 |
438ff85f4dc34721b643497a81fb6384a438986e09c66b3aaaafaee4d287f9d1
|
Provenance
The following attestation bundles were made for aws_cdk_github_oidc-4.2.5.tar.gz:
Publisher:
release.yml on aripalo/aws-cdk-github-oidc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aws_cdk_github_oidc-4.2.5.tar.gz -
Subject digest:
abea4e53dc91edbccb64042917481fa1c2226981817ce24efa5a2309a12d9b06 - Sigstore transparency entry: 1572261690
- Sigstore integration time:
-
Permalink:
aripalo/aws-cdk-github-oidc@3b5e77ad03f06ab68ba84b74e04f6f00f0cf5b04 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/aripalo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3b5e77ad03f06ab68ba84b74e04f6f00f0cf5b04 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aws_cdk_github_oidc-4.2.5-py3-none-any.whl.
File metadata
- Download URL: aws_cdk_github_oidc-4.2.5-py3-none-any.whl
- Upload date:
- Size: 119.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b77e566452fd7e1a60755bc25ac4de04009fd2872b219d7f68b169992caa61f
|
|
| MD5 |
88618ce3f28626f0f2c62723bad75e8e
|
|
| BLAKE2b-256 |
284da87a64b365ed270ccb0cb2849f5dfdca41bde7bc9d4ea30a0cc347efdf14
|
Provenance
The following attestation bundles were made for aws_cdk_github_oidc-4.2.5-py3-none-any.whl:
Publisher:
release.yml on aripalo/aws-cdk-github-oidc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aws_cdk_github_oidc-4.2.5-py3-none-any.whl -
Subject digest:
1b77e566452fd7e1a60755bc25ac4de04009fd2872b219d7f68b169992caa61f - Sigstore transparency entry: 1572261671
- Sigstore integration time:
-
Permalink:
aripalo/aws-cdk-github-oidc@3b5e77ad03f06ab68ba84b74e04f6f00f0cf5b04 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/aripalo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3b5e77ad03f06ab68ba84b74e04f6f00f0cf5b04 -
Trigger Event:
push
-
Statement type: