💻

【AWS】OIDCを作成してGitHubActionsからAWSへアクセスできるようにする

2025/01/03に公開

GitHub Actions で AWS へアクセスする際に認証をどう突破するかが重要です。

GitHub の Actions secrets and variables から AWS の認証情報を読み込む方法もありますが、

機密情報を持たせておくのはセキュリティ的にはよろしくない

そこで、AWS で OIDC(OpenID Connect)を作成し、使用することを考えます。

OIDC は AWS コンソールを使用して作成します。

Terraform で作成したい方はこちら

https://zenn.dev/kuuki/articles/aws-create-oidc-terraform/

OIDC(OpenID Connect)とは

こちらの記事がわかりやすいと思います

https://qiita.com/TakahikoKawasaki/items/498ca08bbfcc341691fe

AWS ドキュメントはこちらです。

https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_roles_providers_create_oidc.html

GitHub のドキュメントはこちら

https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect

OIDC(OpenID Connect)を作成

ID プロバイダを作成

IAM コンソール > ID プロバイダ > プロバイダを追加で作成画面に遷移する

作成画面にて、各設定を入れていきます

プロバイダのタイプ OpenID Connect
プロバイダの URL (入力後 「サムプリントを取得」 をクリック) https://token.actions.githubusercontent.com
対象者 sts.amazonaws.com

プロバイダの URL を入力後、「サムプリントを取得」をクリックするとサムプリントが表示される

右下の**「プロバイダを追加」**をクリックして、ID プロバイダを確認します。

IAM ロールの作成

IAM コンソール > ロール > ロールを作成をクリック

IAM ロールの設定をしていきます

信頼されたエンティティタイプ カスタム信頼ポリシー
カスタム信頼ポリシー 下記 JSON にて必要な情報を置換して入力
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::<AWSアカウントID>:oidc-provider/token.actions.githubusercontent.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringLike": {
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
          "token.actions.githubusercontent.com:sub": "repo:<GitHubユーザー名>/<GitHubリポジトリ名>:*"
        }
      }
    }
  ]
}

Condition にて、指定したリポジトリからのアクセスのみ許可するようにしています

どの操作を許可するか選んで、**「次へ」**をクリックします。

要件に合わせて適宜ポリシーをアタッチしてください。

今回はとりあえず、AmazonS3ReadOnlyAccessを指定しておきます

名前、説明を入力し、ポリシーが適切か確認します。OK なら**「ロールを作成」**をクリック

名前や説明は自分がわかりやすいものであればなんでもいいです。

私はこんな感じにしました。

名前 GitHubActionsOIDCRole
説明 GitHubActions use this role to access to aws

これで AWS 側の準備が完了したので GitHubActions を使用してテストしてみます

GitHub Actions で AWS にアクセスできるかテスト

任意のリポジトリから AWS にアクセスできるかテストしてみます!

テスト用に oidc_test というリポジトリを作成したので、それを使っていきます。

main ブランチからテスト用の test ブランチをきります

oidc_test$ git branch
* main
oidc_test$ git checkout -b test
Switched to a new branch 'test'
oidc_test$ git branch
  main
* test

GitHub Actions のワークフローを定義するため.github/workflows/aws_access_test.ymlを作成します

.github/workflows/ 配下の YAML ファイルに ワークフローを定義することで GitHub Actions を使用できます

// ディレクトリ
terraform_test
 ├── .github
 │ └── workflows
 │   └── aws_access_test.yml   // 作成
 └── README.md

こちらを参考に aws_access_test.yml に記述していきます

name: AWS Test
on:
  push:
    branches:
      # 作業ブランチ
      - test

env:
  # ×××××××××:AWSアカウントID
  # GitHubActionsOIDCRole:作成したIAMロール名
  AWS_ROLE_ARN: arn:aws:iam::×××××××××:role/GitHubActionsOIDCRole

permissions:
  id-token: write
  contents: read
jobs:
  aws-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: aws-actions/configure-aws-credentials@v1
        with:
          role-to-assume: ${{ env.AWS_ROLE_ARN }}
          aws-region: ap-northeast-1
      - run: aws sts get-caller-identity

test ブランチに push した際に

aws sts get-caller-identity

が実行されるワークフローが定義できました!

このワークフローでGitHub Actions から AWS にアクセスできるか push して確認します

oidc_test$ git add .
oidc_test$ git commit -m "commit aws_access_test.yml"
oidc_test$ git push origin test
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 743 bytes | 743.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'test' on GitHub by visiting:
remote:      https://github.com/×××××××××/oidc_test/pull/new/test
remote:
To https://github.com/×××××××××/oidc_test.git
 * [new branch]      test -> test

GitHub > リポジトリ > Actions から結果を確認できます

無事成功していますね!

参考

https://zenn.dev/kou_pg_0131/articles/gh-actions-oidc-aws

https://dev.classmethod.jp/articles/create-iam-id-provider-for-github-actions-with-management-console/

Discussion