概要
AWSでGitHubのOIDC (OpenID Connect) プロバイダーを使用して認証を行うようにすることで、アクセスキーなどの認証情報をGitHubに保存することなく、GitHubからAWSのリソースにアクセスすることができるようになります。
使用するモジュール
Terraform Registry
terraform.io
locals
locals { github_oidc_role_name = "github-actions-role" }
OIDC Provider
module "iam_github_oidc_provider" { source = "terraform-aws-modules/iam/aws//modules/iam-github-oidc-provider" version = "5.17.0" }
Role
subjects
には任意のGitHubリポジトリを設定します。
// [!code word:example-org/example-repo] module "iam_github_oidc_role" { source = "terraform-aws-modules/iam/aws//modules/iam-github-oidc-role" version = "5.17.0" name = local.github_oidc_role_name subjects = [ "example-org/example-repo:*", ] policies = { additional = aws_iam_policy.github_oidc_role_policy.arn } }
RoleのPolicy
以下の例では全て許可するポリシーになっていますので、適宜修正するようにしましょう。
resource "aws_iam_policy" "github_oidc_role_policy" { name = "${local.github_oidc_role_name}-policy" description = "GitHub actions policy" policy = jsonencode({ Version = "2012-10-17" Statement = [ { "Effect" : "Allow", "Action" : "*", "Resource" : "*" } ] }) }
GitHub Actionsでの認証方法
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: aws-role-session
aws-region: ${{ vars.AWS_REGION }}