11
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Terraform × GitHub Actions】planをBedrockで自動要約 & コード解析を全自動化

Last updated at Posted at 2025-12-26

terraform planは差分に関連する多くの情報を出力してしまう仕様上、変更点が増えるほど読むのが大変です。最近はplanの内容を全部AIに読ませて、具体的になにが変わるかを要約してもらってる人も多いのではないでしょうか。

というわけで、このAI要約をCI/CDに組み込んでしまおうという記事になります。ついでなのでfmt、validate、tflint、tfsecも全部仕込んじゃいます:smirk:

tfsecは現在Trivyへの統合が進んでいますが、本記事では例として利用しています。

動作の流れ

image.png

pull request

  1. fmt / validate が動作
  2. terraform planの内容を出力
  3. tflint / tfsec が動作
  4. Bedrockからplanの出力を読み込み
  5. fmt、validate、tflint、tfsecのチェック内容と、
    terraform planの要約をプルリクエストにコメント

merge

terraform applyが動作

あえてAmazon Bedrockで動かしてますが、ここは普通に何かしらのAIのAPIでも代用可能です。BedrockだとIAMロールが必要になるので、単純に作るだけなら、その他APIのほうが構築の手順を減らせます。

環境の構築

GitHub リポジトリ

下記リポジトリを使えば動かせるようになっています。

1. OIDCの設定

Bedrockを動かすために必要です。ざっと手順だけ。

  1. IDプロバイダを作る
  2. IAMロール作成(信頼ポリシーにGitHubリポジトリを登録)
  3. GitHubにRepository secretsを登録

手順の詳細は下記の記事を参考にしてみてください。

2. Terraformコードの作成

内容はある程度記述があれば、なんでもいいです。私のコードではmain.tfにAWSでVPCにEC2を立てる内容にしています。あとはprovider.tfterraform.tfを配置してください。

ついでにこの時点で、tflintとtfsecのファイルも用意しておきましょう。

3. workflow.yaml

GitHub Actionsを動かすために記述します。「動作の流れ」に書いた通りの内容で記述していきます。長いので主要な箇所だけ抜き出します。

workflow.yaml
name: Terraform CI/CD with AI Summary

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]

env:
  AWS_REGION: ap-northeast-1
  BEDROCK_MODEL_ID: jp.anthropic.claude-sonnet-4-5-20250929-v1:0

jobs:
  terraform-check:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      id-token: write
    steps:
      # セットアップ
      - uses: actions/checkout@v6.0.1
      - uses: aws-actions/configure-aws-credentials@v5.1.1
        with:
          role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
          aws-region: ${{ env.AWS_REGION }}
      - uses: hashicorp/setup-terraform@v3.1.2
      - uses: terraform-linters/setup-tflint@v6.2.1

      # Terraformチェック
      - name: Terraform Check & Plan
        run: |
          # ...
        continue-on-error: true

      # Linter & セキュリティ
      - name: TFLint & tfsec
        run: |
          # ...
        continue-on-error: true

      # AI要約生成
      - name: Generate AI Summary
        run: |
          python3 << 'EOF'
          # ...
          EOF

      # PRコメント投稿
      - uses: actions/github-script@v8
        with:
          script: |
            # ...

  terraform-apply:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v6.0.1
      - uses: aws-actions/configure-aws-credentials@v5.1.1
        with:
          role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
          aws-region: ${{ env.AWS_REGION }}
      - uses: hashicorp/setup-terraform@v3.1.2
      - run: terraform init && terraform apply -auto-approve

モデルはAWSコンソールからそのままコピペだと使えないので、頭にjp.をつけて、jp.anthropic.claude-sonnet-4-5-20250929-v1:0としています。

動かしてみる(pull request / terraform plan)

こんなかんじでコメントされました。プロンプトはとくに細かく与えませんでしたが、わりといい感じに要約してくれてます。

image.png

実際のプルリクエストはこちら

以上です。

要約はAIの得意とするところなので、相性もよさそうでした。ぜひ自分用にカスタマイズして使ってみてください🧠

11
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?