この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
2022/1/8 aws-actions/configure-aws-credentials@v1アップデートを反映しました https://dev.classmethod.jp/articles/aws-configure-credentials-v1
吉川@広島です。
[GitHub Actions]aws-actions/configure-aws-credentialsを使ってOIDCプロバイダを介したSwitchRoleをする | DevelopersIO
先日こちらのブログを書いたのですが、色々と変わったため、最新内容を反映した手順を新しくブログにすることにしました(古い記事には注釈を入れておきます)。
IDプロバイダを作成
まずIAM ID Providerを作成します。
- プロバイダのタイプ: OpenIDConnect
- プロバイダのURL: https://token.actions.githubusercontent.com
- 対象者: sts.amazonaws.com
configure-aws-credentialsのアップデートにより、audienceがsigstoreからsts.amazonaws.comに変わりました(ソースコード)。
IAMロールを作成
任意のポリシーを当ててIAMロールを作成します。
作成したら、信頼関係を編集します。
内容は以下のようにします。
{
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::{AWS_ACCOUNT_ID}:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:{GITHUB_ORG_NAME}/{GITHUB_REPO_NAME}:*"
}
}
}
]
}
{}
で括った変数にしている箇所は各自適切な値に置換してください。
この記述はaws-actions/configure-aws-credentials: Configure AWS credential environment variables for use in other GitHub Actions.のSample IAM Role CloudFormation Templateを参考にしています。
ここまででAWS側の準備はOKです。
GitHub Actions Workflowファイルを作成
ファイル名はAWSに何かデプロイするという仮定でaws-deploy.ymlとします。
# .github/workflows/aws-deploy.yml
name: AWS Deploy
on: push
env:
AWS_ROLE_ARN: arn:aws:iam::xxxxxxxxxxxx:role/xxxxxxxxx # 作成したIAMロールのARN
permissions:
id-token: write
contents: read
jobs:
aws-deploy:
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
動作確認
AssumeRoleできました。