naopoyo.com
  • Docs
  • Tags
  • Bookmarks
  • Tools
  • About
  • Docs
  • Tags
  • Bookmarks
  • Tools
  • About

目次

© naopoyo

🏗️

TerraformでAWSにGitHub Actions用のOIDCプロバイダーを作成する

公開日2年以上前2023-10-17
更新日1年以上前2024-07-12
履歴GitHubで見る
  • AWS
  • Terraform
  • GitHub
preview

概要

AWSでGitHubのOIDC (OpenID Connect) プロバイダーを使用して認証を行うようにすることで、アクセスキーなどの認証情報をGitHubに保存することなく、GitHubからAWSのリソースにアクセスすることができるようになります。

使用するモジュール

Terraform Registry
terraform.io favicon
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 }}

目次

最近更新された記事

📐

開発スタック一覧表

2日前·2026年01月25日
  • 一覧表
  • 開発環境
🙃

Tailwind CSS メモ

3日前·2026年01月24日
  • Tailwind
  • CSS
🍕

Next.js + Vitest の環境構築手順

4日前·2026年01月23日
  • Vitest
  • Next.js