こんにちは。AWS CLIが好きな福島です。
- はじめに
- 参考
- 概要図
- やること
- ①IDプロバイダーの設定
- ②IDプロバイダーへのIAMロールの割り当て
- ③②で作成したIAMロールの信頼関係の編集
- ④GitHubのSecretsにIAMロールのARNを保存
- ⑤GitHub ActionsによるGitHubからS3へのオブジェクトコピー
- 終わりに
はじめに
GitHub ActionsでIAMロールを利用してAWSへアクセスできることを知ったため、試してみたいと思います。
IAMロールを利用することでIAMのクレデンシャルの管理が不要なため、セキュアにGitHub Actionsを利用できます。
参考
概要図
認証/認可周りは疎いのですが、概要図は以下のイメージになるかと思います。
やること
①IDプロバイダーの設定
②IDプロバイダーへのIAMロールの割り当て
③②で作成したIAMロールの信頼関係の編集
④GitHubのSecretsにIAMロールのARNを保存
⑤GitHub ActionsによるGitHubからS3へのオブジェクトコピー
①IDプロバイダーの設定
- 「プロバイダー」を追加を押下
- OpenID Connectを選択し、以下の情報を入力後、「プロバイダーを追加」を押下
プロバイダーのURL: https://token.actions.githubusercontent.com
対象者: sts.amazonaws.com
②IDプロバイダーへのIAMロールの割り当て
- IDプロバイダー作成後、右上に表示されている「ロールの割り当て」を押下
「新しいロールを作成」を選択し、「次へ」を押下
Audienceを選択し、次のステップを押下
- IAMロールにアタッチするポリシーを選択し、次のステップを押下
今回は、S3FullAccessをアタッチしますが、本来はGitHub Actionsで必要な権限を適宜割り当てます。
- タグのところはスキップし、最後にIAMロール名を設定し、「ロールの作成」を押下
今回は、S3FullAccessForGitHubActionsを付与しました。
③②で作成したIAMロールの信頼関係の編集
- デフォルトでは、信頼関係は以下の通りとなっております。
この場合、仮に私のIAMロールのARNが漏洩した際に、私以外の人がGitHub Actions経由でAWSを不正に操作することができてしまいます。
- よりセキュアにするため、信頼関係を編集し、特定の組織の特定のリポジトリからのみIAMロールを利用できるように変更します。
[AWSアカウントID]と[組織名]、[リポジトリ名]を適切な値に変更します。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "arn:aws:iam::[AWSアカウントID]:oidc-provider/token.actions.githubusercontent.com" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringLike": { "token.actions.githubusercontent.com:sub": "repo:[組織名]/[リポジトリ名]:*" }, "StringEquals": { "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" } } } ] }
④GitHubのSecretsにIAMロールのARNを保存
- Settings、Actions、New repository secretを押下
- 任意の名前およびIAMロールのARNを設定
⑤GitHub ActionsによるGitHubからS3へのオブジェクトコピー
- GitHub Actionsのymlファイルの作成
[main] # git clone git@github.com:[組織名]/[リポジトリ名].git [main] # cd [リポジトリ名] [main] # mkdir -p .github/workflows [main] # cd .github/workflows [main] # vi aws-github-actions.yml
ファイルの中身は以下の通りで<example-bucket-name>、<example-aws-region>を任意の値に変更します。
# Sample workflow to access AWS resources when workflow is tied to branch # The workflow Creates static website using aws s3 name: AWS example workflow on: push env: BUCKET_NAME : "<example-bucket-name>" AWS_REGION : "<example-aws-region>" # permission can be added at job level or workflow level permissions: id-token: write # This is required for requesting the JWT contents: read # This is required for actions/checkout jobs: S3PackageUpload: runs-on: ubuntu-latest steps: - name: Git clone the repository uses: actions/checkout@v3 - name: configure aws credentials uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.IAM_ROLE_ARN }} role-session-name: samplerolesession aws-region: ${{ env.AWS_REGION }} # Upload a file to AWS s3 - name: Copy index.html to s3 run: | aws s3 cp ./index.html s3://${{ env.BUCKET_NAME }}/
- GitHub Actionsの設定
aws-github-actions.ymlをPushすることでGitHub Actionsの準備が完了です。
[main] # git add aws-github-actions.yml [main] # git commit -m "Add GitHub Actions for aws" [main (root-commit) 5cb0f7e] Add GitHub Actions for aws 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/aws-github-actions.yml [main] # git push Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 839 bytes | 209.00 KiB/s, done. Total 5 (delta 0), reused 0 (delta 0) To github.com:kazuya9831/fk-git.git * [new branch] main -> main [main] #
- GitHub Actionsの動作確認
index.htmlファイルを作成し、Pushします。
[main] # cd ../../ [main] # echo "GitHub Actions" > index.html [main] # git add index.html [main] # git commit -m "Add index.html" [main a1e947a] Add index.html 1 file changed, 1 insertion(+) create mode 100644 index.html [main] # git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:kazuya9831/fk-git.git 5cb0f7e..a1e947a main-> main [main] #
- GitHub Actionsのステータス確認
GitHubのコンソールからGitHub Actionsが正常終了していることを確認できます。
- S3の確認
S3にも無事にファイルがコピーされていることが分かります。
終わりに
今回は、GitHub ActionsでIAMロールを利用してAWSへアクセスしてみました。
どなたかのお役に立てれば幸いです。