「Github ActionsのOIDC連携用のIAM RoleもCDKで設定したい。。」
Github ActionsでIAM Roleを使って、認証できるの嬉しいですよね。
AWS CDKでGithub ActionsのOIDC連携用のIAM Roleを作ってみました。 ConstrutHubに便利なライブラリがあったので、併せて紹介します。
aws-cdk-github-oidc 2.2.1 - Construct Hub
やってみた
AWS CDKの用意(普通に書いてみる)
import { Stack, StackProps, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as iam from 'aws-cdk-lib/aws-iam';
export class GithubActionsOidcStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const provider = new iam.OpenIdConnectProvider(this, 'Provider', {
url: 'https://token.actions.githubusercontent.com',
thumbprints: ['a031c46782e6e6c662c2c87c76da9aa62ccabd8e', '6938fd4d98bab03faadb97b34396831e3780aea1'],
clientIds: ['sts.amazonaws.com'],
});
const role = new iam.Role(this, 'Role', {
roleName: 'DeployRole',
maxSessionDuration: Duration.hours(2),
assumedBy: new iam.WebIdentityPrincipal(provider.openIdConnectProviderArn, {
StringEquals: {
['token.actions.githubusercontent.com:aud']: 'sts.amazonaws.com',
},
StringLike: {
['token.actions.githubusercontent.com:sub']: 'repo:octo-org/octo-repo:*',
},
}),
});
role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess'));
}
}
注意点としては、IAMのConditionでリポジトリ名を指定しているのところです。(StringLike
)
公式ドキュメントでは、StringEquals
を使用しています。
以下、引用。
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:ref:refs/heads/octo-branch"
}
}
今回のようにワイルドカード('repo:octo-org/octo-repo:*'
)を使用する場合は、StringLike
を使用する必要があります。
動作確認
適当なworkflowsファイルを用意して、GithubActionsを実行します。
事前にリポジトリのシークレットにIAMロールのARNを登録する必要があります。 以下の記事がわかりやすいです。
.github/workflows/test.yaml
name: test
on:
push:
jobs:
deploy:
name: List to Amazon S3
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials from Test account
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME_TEST }}
aws-region: ap-northeast-1
- name: Copy files to the test website with the AWS CLI
run: |
aws s3 ls
Github上から確認すると、正常に動作しました。
aws-cdk-github-oidcを使ってみる
aws-cdk-github-oidcを使用すると、すっきり書けます。
thumbprintを、自分で書かなくていいのも嬉しいですね。
以前thumprintが更新されて、IDプロバイダ側で新しいthumprintを追加していなかったためGithubActionsでIAM Roleが使えなくなった経験があります。
GitHub Actions で AWS の認証に使っている OIDC provider の thmbprint が更新された場合の対処 - 継続は力なり
こちらのライブラリを使っていれば、更新時に自分で記述追加しなくてもメンテされていればライブラリのバージョン上げるだけで良さそうです。
import { Stack, StackProps, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { GithubActionsIdentityProvider, GithubActionsRole } from 'aws-cdk-github-oidc';
import * as iam from 'aws-cdk-lib/aws-iam';
export class GithubActionsOidcStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const provider = new GithubActionsIdentityProvider(this, 'GithubProvider');
const role = new GithubActionsRole(this, 'DeployRole', {
provider: provider,
owner: 'octo-org',
repo: 'octo-repo',
roleName: 'DeployRole',
maxSessionDuration: Duration.hours(2),
});
role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess'));
}
}
おわりに
Github ActionsのOIDC連携用のIAM RoleもAWS CDKで作ってみました。
実施する頻度は多くないかもしれませんが、設定項目が多く手動だと面倒なのでコード化しておくと便利かもしれません。