LoginSignup
1
0

More than 1 year has passed since last update.

GitHub Actionsで何故かIAM Roleの引受ができない

Posted at

仕事でよくGithub Actionsを使用するのですが、忘れがちな設定なのでメモします。

やりたきこと

  • GitHub Actions内で、IAMロールをOIDCを使用してActions実行インスタンスに渡したい。
  • 設定値

IAMロール側信頼ポリシー

{
    "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:sub": "repo:<GitHubアカウント名>/<リポジトリ名>:*"
                }
            }
        }
    ]
}

IDプロバイダ

こちらの記事を参考にIDプロバイダの作成も済ませる。

GitHub Actionsワークフローファイル

name: Sample

on:
  workflow_dispatch:

env:
  AWS_REGION: ap-northeast-1
  AWS_ROLE_ARN: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/Sample-role

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@master
        with:
          role-to-assume: ${{ env.AWS_ROLE_ARN }}
          aws-region: ${{ env.AWS_REGION }}

AWSのアカウントIDは一応リポジトリのSecretに追加して運用してます。

エラー内容

スクリーンショット 2023-02-03 12.36.27.png
このCredentials could not be loaded, please check your action inputs: Could not load credentials from any providersというエラーがなかなか辛かった。

修正方法

jobs:
  deploy:
    runs-on: ubuntu-latest
    # These permissions are needed to interact with GitHub's OIDC Token endpoint.
    permissions:
      id-token: write
      contents: read

こんな感じで、id-tokenに書き込むための設定を加えてあげる。
じつはGitHubのUsageにちゃんと書いてある。
やっぱり一次情報を見に行くのが鉄板ですね。

1
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
1
0