• terraform.tf(main.tf)
    • data は定義済みの値を参照する
    • resourceは新規の定義

https://zenn.dev/1mono2/articles/9c7ce52003319d codeconnectionでの接続を確立できている場合は認証情報が不要説あり

provider "aws" {
  region = "us-west-2"  # 適切なリージョンに変更してください
}

# GitHubトークンをSecrets Managerから取得
data "aws_secretsmanager_secret" "github_token" {
  name = "github-token"
}

data "aws_secretsmanager_secret_version" "github_token_version" {
  secret_id = data.aws_secretsmanager_secret.github_token.id
}

# S3バケットの作成(テスト結果用)
resource "aws_s3_bucket" "test_results_bucket" {
  bucket = "e2e-test-results-bucket"  # 適切なバケット名に変更してください
}

# CodeBuildプロジェクトの定義
resource "aws_codebuild_project" "e2e_test" {
  name          = "e2e-test-project"
  description   = "CodeBuild project for running Playwright E2E tests"
  build_timeout = 60

  source {
    type            = "GITHUB"
    location        = "https://github.com/your-org/your-repo.git"  # 適切なリポジトリに変更してください
    git_clone_depth = 1
    auth {
      type     = "OAUTH"
      resource = data.aws_secretsmanager_secret_version.github_token_version.secret_string
    }
  }

  artifacts {
    type = "NO_ARTIFACTS"
  }

  environment {
    compute_type                = "BUILD_GENERAL1_SMALL"
    image                       = "aws/codebuild/standard:5.0"
    type                        = "LINUX_CONTAINER"
    environment_variable {
      name  = "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD"
      value = "1"
    }
    environment_variable {
      name  = "TEST_ENV"
      value = ""  # ここはGitHub Actionsから渡される
    }
    environment_variable {
      name  = "S3_BUCKET"
      value = aws_s3_bucket.test_results_bucket.bucket
    }
  }

  service_role = aws_iam_role.codebuild_role.arn
}

# CodeBuild用のIAMロール
resource "aws_iam_role" "codebuild_role" {
  name = "codebuild-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Principal = {
          Service = "codebuild.amazonaws.com"
        }
        Action = "sts:AssumeRole"
      }
    ]
  })
}

resource "aws_iam_role_policy" "codebuild_policy" {
  name   = "codebuild-policy"
  role   = aws_iam_role.codebuild_role.id
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Action = [
          "logs:CreateLogGroup",
          "logs:CreateLogStream",
          "logs:PutLogEvents",
          "s3:*",
          "codebuild:*"
        ]
        Resource = "*"
      },
      {
        Effect = "Allow"
        Action = [
          "s3:PutObject",
          "s3:PutObjectAcl"
        ]
        Resource = "${aws_s3_bucket.test_results_bucket.arn}/*"
      }
    ]
  })
}

# CodePipeline用のIAMロール
resource "aws_iam_role" "codepipeline_role" {
  name = "codepipeline-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Principal = {
          Service = "codepipeline.amazonaws.com"
        }
        Action = "sts:AssumeRole"
      }
    ]
  })
}

resource "aws_iam_role_policy" "codepipeline_policy" {
  name   = "codepipeline-policy"
  role   = aws_iam_role.codepipeline_role.id
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Action = [
          "codebuild:BatchGetBuilds",
          "codebuild:StartBuild",
          "codebuild:StopBuild",
          "s3:*",
          "codepipeline:*",
          "iam:PassRole"
        ]
        Resource = "*"
      }
    ]
  })
}

# S3バケットの作成(CodePipelineのアーティファクト用)
resource "aws_s3_bucket" "codepipeline_bucket" {
  bucket = "codepipeline-artifacts-bucket"  # 適切なバケット名に変更してください
}

# CodePipelineの定義
resource "aws_codepipeline" "e2e_pipeline" {
  name     = "e2e-test-pipeline"
  role_arn = aws_iam_role.codepipeline_role.arn

  artifact_store {
    type     = "S3"
    location = aws_s3_bucket.codepipeline_bucket.bucket
  }

  stage {
    name = "Source"

    action {
      name             = "Source"
      category         = "Source"
      owner            = "ThirdParty"
      provider         = "GitHub"
      version          = "1"
      output_artifacts = ["source_output"]

      configuration = {
        Owner      = "your-org"
        Repo       = "your-repo"
        Branch     = "main"
        OAuthToken = data.aws_secretsmanager_secret_version.github_token_version.secret_string
      }
    }
  }

  stage {
    name = "Build"

    action {
      name             = "Build"
      category         = "Build"
      owner            = "AWS"
      provider         = "CodeBuild"
      input_artifacts  = ["source_output"]
      output_artifacts = ["build_output"]
      version          = "1"

      configuration = {
        ProjectName = aws_codebuild_project.e2e_test.name
      }
    }
  }
}
  • AWS Secrets Managerの定義は直接行われるべき
    • 一応terraform上での定義もできるが基本的にはセキュリティ上の問題から直接行われる
resource "aws_secretsmanager_secret_version" "github_token_version" {
  secret_id     = aws_secretsmanager_secret.github_token.id
  secret_string = "YOUR_GITHUB_TOKEN"  # ここにシークレット値を直接記述
}
  • buildSpec.yml
version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 14
    commands:
      - npm install
      - npx playwright install
  build:
    commands:
      - echo "Running tests in environment: $TEST_ENV"
      - npx playwright test --reporter=json > test-results.json
  post_build:
    commands:
      - echo "Uploading test results to S3"
      - aws s3 cp test-results.json s3://$S3_BUCKET/test-results/test-results-$(date +%Y-%m-%d_%H-%M-%S).json
  • action.yml
name: Run E2E Tests

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Trigger AWS CodeBuild
        uses: aws-actions/aws-codebuild-run-build@v1
        with:
          project-name: e2e-test-project
          report-build-status: true
          environment-variables: |
            TEST_ENV=${{ secrets.TEST_ENV }}  # GitHub Secretsから環境変数を取得
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: 'us-west-2'  # 適切なリージョンに変更してください

AWS CodePipelineのアーティファクトは、パイプラインの各ステージ間でデータを渡すために使用されるファイルやデータの集合です。アーティファクトは、ソースコード、ビルド成果物、テスト結果など、パイプラインの各ステージで生成されるデータを含むことができます。

アーティファクトの役割 ソースアーティファクト: ソースステージで取得されたソースコードやデータ。 ビルドアーティファクト: ビルドステージで生成されたビルド成果物(例: コンパイルされたバイナリ、パッケージなど)。 テストアーティファクト: テストステージで生成されたテスト結果やログ。 S3バケットの役割 CodePipelineは、アーティファクトを保存するためにS3バケットを使用します。各ステージ間でアーティファクトを渡すために、S3バケットにアーティファクトをアップロードし、次のステージでそれをダウンロードして使用します。

Terraformでの設定例 以下は、CodePipelineのアーティファクト用にS3バケットを設定し、パイプラインの各ステージでアーティファクトを使用するTerraformの例です。

  1. S3バケットの作成:

aws_s3_bucket リソースを使用して、CodePipelineのアーティファクトを保存するためのS3バケットを作成します。 2. CodePipelineの定義:

aws_codepipeline リソースを使用して、パイプラインを定義します。 artifact_store ブロックで、アーティファクトを保存するS3バケットを指定します。 stage ブロックで、パイプラインの各ステージを定義します。 3. Sourceステージ:

GitHubからソースコードを取得し、source_output という名前のアーティファクトとして保存します。 4. Buildステージ:

source_output アーティファクトを入力として受け取り、CodeBuildプロジェクトを実行します。 ビルド結果を build_output アーティファクトとして保存します。 この設定により、CodePipelineはGitHubリポジトリからソースコードを取得し、CodeBuildプロジェクトを実行してビルド成果物を生成します。各ステージ間でアーティファクトを使用してデータを渡すことで、パイプライン全体のプロセスを管理します。