Claude Code GitHub Actions上でTerraformを実行してみる(バックエンド HCP Terraform)
GitHub Issueのコメントで依頼して、Claude Code GitHub ActionsでTerraformコードを生成することを試していました。
生成されたコードが初回のPlanやポリシーチェックでエラーになることがありました。
Claude Code側で自発的にPlanやポリシーチェックを行い修正してもらいたかったので、Claude Code GitHub Actionsでterraform cliを実行できるように設定してみました。
GitHub ActionsワークフローにTerraform CLIをセットアップ
Claude Code GitHub Actions用のワークフローファイルにTerraform CLIのセットアップを追加します。
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 }}
これでワークフロー内にTerraform CLIがインストールされます。
今回はHCP Terraformを利用するため、cli_config_credentials_tokenでHCP Terraformのトークンを渡しています。
HCP Terraformのアカウントで生成したAPIトークンを使用して認証します。詳細な設定手順は以下を参照してください。
Claude Codeにterraformコマンドの利用を許可
Claude Code GitHub Actionsでは利用できるツールが制限されいます。
デフォルトのままでは、Claude CodeがTerraform cliを使えないため、オプション(allowed-tools)で許可を追加します。
Plan結果を確認するために、terraform initとterraform planを許可します。
追加が完了したら、GitHubにPushします。
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)"
動作確認
GitHub Issue上で以下のプロンプトを実行しました。
@claude
terraform planを実行して、結果を教えて下さい。
Plan結果を教えてくれました。


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

あえて、Planが失敗するようにTerraformコードを書き換えて、Claude Code GitHub Actionsで修正してくれるか確認してみます。
以下のコードをGitHubにPushします。
# 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を作成してください。



修正が適切に行われました。
一応再度Planを実行してもらい、Planが通ることを確認できました。
@claude
修正したブランチでterraform planを実行して、エラーがでないか確認してください。









