Claude Code GitHub Actions上でTerraformを実行してみる(バックエンド HCP Terraform)

Claude Code GitHub Actions上でTerraformを実行してみる(バックエンド HCP Terraform)

2025.11.23

GitHub Issueのコメントで依頼して、Claude Code GitHub ActionsでTerraformコードを生成することを試していました。

https://dev.classmethod.jp/articles/hahitalks-japan-2025-genai-terraform-workflow/

生成されたコードが初回のPlanやポリシーチェックでエラーになることがありました。

Claude Code側で自発的にPlanやポリシーチェックを行い修正してもらいたかったので、Claude Code GitHub Actionsでterraform cliを実行できるように設定してみました。

GitHub ActionsワークフローにTerraform CLIをセットアップ

Claude Code GitHub Actions用のワークフローファイルにTerraform CLIのセットアップを追加します。

.github/workflows/claude.yml
name: Claude Code

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    if: |
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
      issues: read
      id-token: write
      actions: read # Required for Claude to read CI results on PRs
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

+      - name: Setup Terraform
+        uses: hashicorp/setup-terraform@v3
+        with:
+          cli_config_credentials_token: ${{ secrets.TFE_TOKEN }}

https://github.com/hashicorp/setup-terraform

これでワークフロー内にTerraform CLIがインストールされます。

今回はHCP Terraformを利用するため、cli_config_credentials_tokenでHCP Terraformのトークンを渡しています。

HCP Terraformのアカウントで生成したAPIトークンを使用して認証します。詳細な設定手順は以下を参照してください。

https://dev.classmethod.jp/articles/claude-code-github-actions-terraform-mcp-hcptf/

Claude Codeにterraformコマンドの利用を許可

Claude Code GitHub Actionsでは利用できるツールが制限されいます。

デフォルトのままでは、Claude CodeがTerraform cliを使えないため、オプション(allowed-tools)で許可を追加します。

Plan結果を確認するために、terraform initterraform planを許可します。

追加が完了したら、GitHubにPushします。

.github/workflows/claude.yml
name: Claude Code

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    if: |
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
      issues: read
      id-token: write
      actions: read # Required for Claude to read CI results on PRs
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3
        with:
          cli_config_credentials_token: ${{ secrets.TFE_TOKEN }}

      - name: Run Claude Code
        id: claude
        uses: anthropics/claude-code-action@v1
        with:
          claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
+          claude_args: |
+            --allowed-tools "Bash(terraform init),Bash(terraform plan)"

https://code.claude.com/docs/ja/iam#ツール固有の権限ルール

https://code.claude.com/docs/ja/github-actions#claude-argsの使用

動作確認

GitHub Issue上で以下のプロンプトを実行しました。

@claude

terraform planを実行して、結果を教えて下さい。

Plan結果を教えてくれました。

既存のVPC内にEC2インスタンスを作成する_·_Issue__35_·_msato0731_cc-hcptf-demo.png

既存のVPC内にEC2インスタンスを作成する_·_Issue__35_·_msato0731_cc-hcptf-demo.png

HCP Terraform上でもPlanが行われたことを確認できました。

run-uEW4EfZGkZ32MvTd___Runs___classmethod-sandbox___HCP_Terraform.png

あえて、Planが失敗するようにTerraformコードを書き換えて、Claude Code GitHub Actionsで修正してくれるか確認してみます。

以下のコードをGitHubにPushします。

main.tf
# VPC Module
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 6.0.1"

  name = "${var.environment}-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["ap-northeast-1a", "ap-northeast-1c"]
  private_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
  public_subnets  = ["10.0.1.0/24", "10.0.2.0/24"]

  enable_nat_gateway   = false
  enable_vpn_gateway   = false
  enable_dns_hostnames = true
  enable_dns_support   = true

  # 動作確認用:存在しない変数参照でエラーを発生
  tags = {
    Environment = var.non_existent_variable
  }
}

Issueのコメントで以下のプロンプトを実行します。

@claude

terraform planを実行して、エラーが出る場合は修正のPRを作成してください。

テスト_terraform_plan_·_Issue__36_·_msato0731_cc-hcptf-demo.png

run-zRHiQDTuJqFrmwBi___Runs___classmethod-sandbox___HCP_Terraform.png

Comparing_main___claude_issue-36-20251121-0802_·_msato0731_cc-hcptf-demo.png

修正が適切に行われました。

一応再度Planを実行してもらい、Planが通ることを確認できました。

@claude

修正したブランチでterraform planを実行して、エラーがでないか確認してください。

run-wRGxaRJ5z63YjvGY___Runs___classmethod-sandbox___HCP_Terraform.png

この記事をシェアする

FacebookHatena blogX

関連記事