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.
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:
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
[!TIP]
Currently only one filter can be defined, a future update will introduce option to provide multiple filters (on the same repo).
Give both ownerId and repoId to form the trust policy with an immutable subject:
const deployRole = new GithubActionsRole(scope, "DeployRole", {
provider: provider,
owner: "octo-org",
repo: "octo-repo",
ownerId: "123456", // your repository owner ID
repoId: "456789", // your repository ID
filter: "ref:refs/tags/v*",
});
Which results in a subject condition of repo:octo-org@123456/octo-repo@456789:ref:refs/tags/v* instead of repo:octo-org/octo-repo:ref:refs/tags/v*. Both properties must be given together, as CDK will fail if you only provide one of them.
[!IMPORTANT]
Ensure Github actually issues an immutable subject for your repository before deploying a role which requires one, as otherwise sts:AssumeRoleWithWebIdentity will be denied.
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.
Once the RETAIN removal policy has been successfully deployed, upgrade this library to v4.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 GithubProviderReference instead of GithubProvider). 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.
Check pnpm exec cdk diff which should look similar to: