【github actions】OIDCでAWSと連携する
IDプロバイダ作成
table:必要なもの
プロバイダURL token.actions.githubusercontent.com
Audience sts.amazonaws.com
IAMロール作成
これで動いた
conditionでブランチを指定したりもできる様子
code:IAMロールの信頼関係.json
{
"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:<githubアカウント名>/<githubリポジトリ名>:*"
}
}
}
]
}
ポリシーは適当に作成/管理したいリソースのもの
actionsの権限設定
code:workflow.yaml
permissions:
id-token: write # ワークフローの実行に必要
contents: read # actions/checkoutに必要
AWSの認証情報取得
code:workflow.yaml
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: <IAMロールのarn>
aws-region: <リージョン>
動確コード(要S3のlist権限)
code:workflow.yaml
name: test
on:
workflow_dispatch:
env:
AWS_ROLE_ARN: ${{ secrets.AWS_OIDC_ROLE }} # 作成したIAMロールのARN
permissions:
id-token: write
contents: read
jobs:
aws-deploy-terraform:
runs-on: ubuntu-latest
steps:
- name: configure aws credential
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ secrets.AWS_OIDC_ROLE }}
aws-region: ap-northeast-1
- name: OIDC test
run: aws s3 ls
参考
code:保留.md
- OIDCとはそもそも何か
- audienceって何だ