0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub ActionsでOIDCを使うときは、id_tokenの書き込み権限をつけましょう

Last updated at Posted at 2025-04-28

やりたいこと

GitHub Actionsのワークフロー内でOIDCを利用し、AWS側で作成したIAMロールをAssumeする

現象

image.png

It looks like...のメッセージと共にクレデンシャルをの取得に失敗した旨のエラーが表示される。

確認したこと

  • あらかじめ登録しているSecretsVariablesの値は間違い無い
  • AWS側でのOIDCプロバイダも作成済
  • GitHub ActionsがAssumeするロールの信頼ポリシーで対象リポジトリを信頼している

修正前のコード

name: AWS OIDC

on:
  push:

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Set Environment Values
        run: |
            echo "AWS_ACCOUNT_ID=${{ secrets.AWS_ACCOUNT_ID }}" >> $GITHUB_ENV
            echo "AWS_CICD_ROLE_NAME=${{ secrets.AWS_CICD_ROLE_NAME }}" >> $GITHUB_ENV
            echo "AWS_REGION=${{ secrets.AWS_REGION }}" >> $GITHUB_ENV
      - name: Get AWS Temporary Credential
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: '${{ env.AWS_REGION }}'
          role-to-assume: 'arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/${{ env.AWS_CICD_ROLE_NAME }}'

※regionもsecretsで登録しているのは気にしないでください

原因

It looks like you might be trying to authenticate with OIDC. Did you mean to set the `id-token` permission? If you are not trying to authenticate with OIDC and the action is working successfully, you can ignore this message.

というメッセージの通り、id-tokenのパーミッションが設定されていないのが原因。
以下の通り、GitHub Actionsのデフォルト設定だと、id-tokenの書き込み権限が無効になっている。

If you don't specify permissions, the GITHUB_TOKEN only has read access to the contents and metadata of the repository.
Other scopes, such as id-token, are not granted unless explicitly requested.

そのため、aws-actions/configure-aws-credentialsの実行の中で必要なGitHubから発行されるOIDCトークンが発行できずエラーになった。

修正後のコード

name: AWS OIDC

on:
  push:

+ permissions:
+   id-token: write

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Set Environment Values
        run: |
            echo "AWS_ACCOUNT_ID=${{ secrets.AWS_ACCOUNT_ID }}" >> $GITHUB_ENV
            echo "AWS_CICD_ROLE_NAME=${{ secrets.AWS_CICD_ROLE_NAME }}" >> $GITHUB_ENV
            echo "AWS_REGION=${{ secrets.AWS_REGION }}" >> $GITHUB_ENV
      - name: Get AWS Temporary Credential
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: '${{ env.AWS_REGION }}'
          role-to-assume: 'arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/${{ env.AWS_CICD_ROLE_NAME }}'

まとめ

GitHub ActionsでOIDCを使うときは、id_tokenの書き込み権限をつけよう

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?