jpskill.com
🛠️ 開発・MCP コミュニティ

Terraformを使ったインフラ構築を支援し、モジュール設計や状態管理、セキュリティ対策、複数リージョン展開、CI/CD連携など、IaCのベストプラクティスに沿った提案やレビューを行うSkill。

terraform-patterns

Terraformのインフラストラクチャをコードで管理するためのエージェントスキルとプラグインで、モジュール設計、状態管理、セキュリティ強化、ポリシー適用、CI/CDワークフローを支援するSkillです。

📜 元の英語説明(参考)

Terraform infrastructure-as-code agent skill and plugin for Claude Code, Codex, Gemini CLI, Cursor, OpenClaw. Covers module design patterns, state management strategies, provider configuration, security hardening, policy-as-code with Sentinel/OPA, and CI/CD plan/apply workflows. Use when: user wants to design Terraform modules, manage state backends, review Terraform security, implement multi-region deployments, or follow IaC best practices.

🇯🇵 日本人クリエイター向け解説

一言でいうと

Terraformのインフラストラクチャをコードで管理するためのエージェントスキルとプラグインで、モジュール設計、状態管理、セキュリティ強化、ポリシー適用、CI/CDワークフローを支援するSkillです。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o terraform-patterns.zip https://jpskill.com/download/5477.zip && unzip -o terraform-patterns.zip && rm terraform-patterns.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/5477.zip -OutFile "$d\terraform-patterns.zip"; Expand-Archive "$d\terraform-patterns.zip" -DestinationPath $d -Force; ri "$d\terraform-patterns.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して terraform-patterns.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → terraform-patterns フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-17
取得日時
2026-05-18
同梱ファイル
5

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Terraform パターン

予測可能なインフラストラクチャ。安全なステート。構成可能なモジュール。ドリフトなし。

散らばった HCL を、構造化され、安全で、本番環境レベルのインフラストラクチャコードに変える、意見のある Terraform ワークフローです。モジュール設計、ステート管理、プロバイダーパターン、セキュリティ強化、CI/CD 統合について説明します。

Terraform のチュートリアルではなく、午前3時に壊れないインフラストラクチャコードの書き方に関する具体的な決定事項のセットです。


スラッシュコマンド

コマンド 内容
/terraform:review Terraform コードをアンチパターン、セキュリティ問題、構造上の問題について分析します
/terraform:module 適切な入力、出力、構成を持つ Terraform モジュールを設計またはリファクタリングします
/terraform:security Terraform コードをセキュリティ脆弱性、シークレットの露出、IAM の設定ミスについて監査します

このスキルが有効になるタイミング

ユーザーからの以下のパターンを認識します。

  • 「この Terraform コードをレビューしてください」
  • 「〜の Terraform モジュールを設計してください」
  • 「私の Terraform ステートは〜」
  • 「リモートステートバックエンドを設定してください」
  • 「マルチリージョン Terraform デプロイ」
  • 「Terraform セキュリティレビュー」
  • 「モジュール構造のベストプラクティス」
  • 「Terraform CI/CD パイプライン」
  • .tf ファイル、HCL、Terraform モジュール、ステート管理、プロバイダー設定、Infrastructure-as-Code を含むあらゆるリクエスト

ユーザーが .tf ファイルを持っている場合、または Terraform でインフラストラクチャをプロビジョニングしたい場合 → このスキルが適用されます。


ワークフロー

/terraform:review — Terraform コードレビュー

  1. 現在の状態を分析する

    • ターゲットディレクトリ内のすべての .tf ファイルを読み取ります
    • モジュール構造(フラットかネストか)を特定します
    • リソース、データソース、変数、出力の数を数えます
    • 命名規則を確認します
  2. レビューチェックリストを適用する

    MODULE STRUCTURE
    ├── Variables have descriptions and type constraints
    ├── Outputs expose only what consumers need
    ├── Resources use consistent naming: {provider}_{type}_{purpose}
    ├── Locals used for computed values and DRY expressions
    └── No hardcoded values — everything parameterized or in locals
    
    STATE & BACKEND
    ├── Remote backend configured (S3, GCS, Azure Blob, Terraform Cloud)
    ├── State locking enabled (DynamoDB for S3, native for others)
    ├── State encryption at rest enabled
    ├── No secrets stored in state (or state access is restricted)
    └── Workspaces or directory isolation for environments
    
    PROVIDERS
    ├── Version constraints use pessimistic operator: ~> 5.0
    ├── Required providers block in terraform {} block
    ├── Provider aliases for multi-region or multi-account
    └── No provider configuration in child modules
    
    SECURITY
    ├── No hardcoded secrets, keys, or passwords
    ├── IAM follows least-privilege principle
    ├── Encryption enabled for storage, databases, secrets
    ├── Security groups are not overly permissive (no 0.0.0.0/0 ingress on sensitive ports)
    └── Sensitive variables marked with sensitive = true
  3. レポートを生成する

    python3 scripts/tf_module_analyzer.py ./terraform
  4. セキュリティスキャンを実行する

    python3 scripts/tf_security_scanner.py ./terraform

/terraform:module — モジュール設計

  1. モジュールのスコープを特定する

    • 単一責任:1つのモジュール = 1つの論理グループ
    • 入力(変数)、出力、リソースの境界を決定します
    • 決定:フラットモジュール(単一ディレクトリ)か、ネストされたモジュール(子モジュールを呼び出す)か
  2. モジュール設計チェックリストを適用する

    STRUCTURE
    ├── main.tf        — Primary resources
    ├── variables.tf   — All input variables with descriptions and types
    ├── outputs.tf     — All outputs with descriptions
    ├── versions.tf    — terraform {} block with required_providers
    ├── locals.tf      — Computed values and naming conventions
    ├── data.tf        — Data sources (if any)
    └── README.md      — Usage examples and variable documentation
    
    VARIABLES
    ├── Every variable has: description, type, validation (where applicable)
    ├── Sensitive values marked: sensitive = true
    ├── Defaults provided for optional settings
    ├── Use object types for related settings: variable "config" { type = object({...}) }
    └── Validate with: validation { condition = ... }
    
    OUTPUTS
    ├── Output IDs, ARNs, endpoints — things consumers need
    ├── Include description on every output
    ├── Mark sensitive outputs: sensitive = true
    └── Don't output entire resources — only specific attributes
    
    COMPOSITION
    ├── Root module calls child modules
    ├── Child modules never call other child modules
    ├── Pass values explicitly — no hidden data source lookups in child modules
    ├── Provider configuration only in root module
    └── Use module "name" { source = "./modules/name" }
  3. モジュールスキャフォールドを生成する

    • ボイラープレートを含むファイル構造を出力します
    • 変数検証ブロックを含めます
    • 適切なライフサイクルルールを追加します

/terraform:security — セキュリティ監査

  1. コードレベル監査

    チェック 重大度 修正
    .tf ファイル内のハードコードされたシークレット 致命的 sensitive = true の変数または Vault を使用する
    * アクションを持つ IAM ポリシー 致命的 特定のアクションとリソースにスコープを限定する
    ポート 22/3389 で 0.0.0.0/0 を持つセキュリティグループ 致命的 既知の CIDR ブロックに制限するか、SSM/踏み台を使用する
    暗号化されていない S3 バケット server_side_encryption_configuration ブロックを追加する
    パブリックアクセスを持つ S3 バケット aws_s3_bucket_public_access_block を追加する
    暗号化されていない RDS storage_encrypted = true を設定する
    公開アクセス可能な RDS publicly_accessible = false を設定する
    CloudTrail が有効になっていない aws_cloudtrail リソースを追加する
    ステートフルリソースに prevent_destroy がない lifecycle { prevent_destroy = true } を追加する
    シークレットに sensitive = true がない変数 シークレット変数に sensitive = true を追加する
  2. ステートセキュリティ監査

    | チェック | 重大度 | 修正 |

📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Terraform Patterns

Predictable infrastructure. Secure state. Modules that compose. No drift.

Opinionated Terraform workflow that turns sprawling HCL into well-structured, secure, production-grade infrastructure code. Covers module design, state management, provider patterns, security hardening, and CI/CD integration.

Not a Terraform tutorial — a set of concrete decisions about how to write infrastructure code that doesn't break at 3 AM.


Slash Commands

Command What it does
/terraform:review Analyze Terraform code for anti-patterns, security issues, and structure problems
/terraform:module Design or refactor a Terraform module with proper inputs, outputs, and composition
/terraform:security Audit Terraform code for security vulnerabilities, secrets exposure, and IAM misconfigurations

When This Skill Activates

Recognize these patterns from the user:

  • "Review this Terraform code"
  • "Design a Terraform module for..."
  • "My Terraform state is..."
  • "Set up remote state backend"
  • "Multi-region Terraform deployment"
  • "Terraform security review"
  • "Module structure best practices"
  • "Terraform CI/CD pipeline"
  • Any request involving: .tf files, HCL, Terraform modules, state management, provider configuration, infrastructure-as-code

If the user has .tf files or wants to provision infrastructure with Terraform → this skill applies.


Workflow

/terraform:review — Terraform Code Review

  1. Analyze current state

    • Read all .tf files in the target directory
    • Identify module structure (flat vs nested)
    • Count resources, data sources, variables, outputs
    • Check naming conventions
  2. Apply review checklist

    MODULE STRUCTURE
    ├── Variables have descriptions and type constraints
    ├── Outputs expose only what consumers need
    ├── Resources use consistent naming: {provider}_{type}_{purpose}
    ├── Locals used for computed values and DRY expressions
    └── No hardcoded values — everything parameterized or in locals
    
    STATE & BACKEND
    ├── Remote backend configured (S3, GCS, Azure Blob, Terraform Cloud)
    ├── State locking enabled (DynamoDB for S3, native for others)
    ├── State encryption at rest enabled
    ├── No secrets stored in state (or state access is restricted)
    └── Workspaces or directory isolation for environments
    
    PROVIDERS
    ├── Version constraints use pessimistic operator: ~> 5.0
    ├── Required providers block in terraform {} block
    ├── Provider aliases for multi-region or multi-account
    └── No provider configuration in child modules
    
    SECURITY
    ├── No hardcoded secrets, keys, or passwords
    ├── IAM follows least-privilege principle
    ├── Encryption enabled for storage, databases, secrets
    ├── Security groups are not overly permissive (no 0.0.0.0/0 ingress on sensitive ports)
    └── Sensitive variables marked with sensitive = true
  3. Generate report

    python3 scripts/tf_module_analyzer.py ./terraform
  4. Run security scan

    python3 scripts/tf_security_scanner.py ./terraform

/terraform:module — Module Design

  1. Identify module scope

    • Single responsibility: one module = one logical grouping
    • Determine inputs (variables), outputs, and resource boundaries
    • Decide: flat module (single directory) vs nested (calling child modules)
  2. Apply module design checklist

    STRUCTURE
    ├── main.tf        — Primary resources
    ├── variables.tf   — All input variables with descriptions and types
    ├── outputs.tf     — All outputs with descriptions
    ├── versions.tf    — terraform {} block with required_providers
    ├── locals.tf      — Computed values and naming conventions
    ├── data.tf        — Data sources (if any)
    └── README.md      — Usage examples and variable documentation
    
    VARIABLES
    ├── Every variable has: description, type, validation (where applicable)
    ├── Sensitive values marked: sensitive = true
    ├── Defaults provided for optional settings
    ├── Use object types for related settings: variable "config" { type = object({...}) }
    └── Validate with: validation { condition = ... }
    
    OUTPUTS
    ├── Output IDs, ARNs, endpoints — things consumers need
    ├── Include description on every output
    ├── Mark sensitive outputs: sensitive = true
    └── Don't output entire resources — only specific attributes
    
    COMPOSITION
    ├── Root module calls child modules
    ├── Child modules never call other child modules
    ├── Pass values explicitly — no hidden data source lookups in child modules
    ├── Provider configuration only in root module
    └── Use module "name" { source = "./modules/name" }
  3. Generate module scaffold

    • Output file structure with boilerplate
    • Include variable validation blocks
    • Add lifecycle rules where appropriate

/terraform:security — Security Audit

  1. Code-level audit

    Check Severity Fix
    Hardcoded secrets in .tf files Critical Use variables with sensitive = true or vault
    IAM policy with * actions Critical Scope to specific actions and resources
    Security group with 0.0.0.0/0 on port 22/3389 Critical Restrict to known CIDR blocks or use SSM/bastion
    S3 bucket without encryption High Add server_side_encryption_configuration block
    S3 bucket with public access High Add aws_s3_bucket_public_access_block
    RDS without encryption High Set storage_encrypted = true
    RDS publicly accessible High Set publicly_accessible = false
    CloudTrail not enabled Medium Add aws_cloudtrail resource
    Missing prevent_destroy on stateful resources Medium Add lifecycle { prevent_destroy = true }
    Variables without sensitive = true for secrets Medium Add sensitive = true to secret variables
  2. State security audit

    Check Severity Fix
    Local state file Critical Migrate to remote backend with encryption
    Remote state without encryption High Enable encryption on backend (SSE-S3, KMS)
    No state locking High Enable DynamoDB for S3, native for TF Cloud
    State accessible to all team members Medium Restrict via IAM policies or TF Cloud teams
  3. Generate security report

    python3 scripts/tf_security_scanner.py ./terraform
    python3 scripts/tf_security_scanner.py ./terraform --output json

Tooling

scripts/tf_module_analyzer.py

CLI utility for analyzing Terraform directory structure and module quality.

Features:

  • Resource and data source counting
  • Variable and output analysis (missing descriptions, types, validation)
  • Naming convention checks
  • Module composition detection
  • File structure validation
  • JSON and text output

Usage:

# Analyze a Terraform directory
python3 scripts/tf_module_analyzer.py ./terraform

# JSON output
python3 scripts/tf_module_analyzer.py ./terraform --output json

# Analyze a specific module
python3 scripts/tf_module_analyzer.py ./modules/vpc

scripts/tf_security_scanner.py

CLI utility for scanning .tf files for common security issues.

Features:

  • Hardcoded secret detection (AWS keys, passwords, tokens)
  • Overly permissive IAM policy detection
  • Open security group detection (0.0.0.0/0 on sensitive ports)
  • Missing encryption checks (S3, RDS, EBS)
  • Public access detection (S3, RDS, EC2)
  • Sensitive variable audit
  • JSON and text output

Usage:

# Scan a Terraform directory
python3 scripts/tf_security_scanner.py ./terraform

# JSON output
python3 scripts/tf_security_scanner.py ./terraform --output json

# Strict mode (elevate warnings)
python3 scripts/tf_security_scanner.py ./terraform --strict

Module Design Patterns

Pattern 1: Flat Module (Small/Medium Projects)

infrastructure/
├── main.tf          # All resources
├── variables.tf     # All inputs
├── outputs.tf       # All outputs
├── versions.tf      # Provider requirements
├── terraform.tfvars # Environment values (not committed)
└── backend.tf       # Remote state configuration

Best for: Single application, < 20 resources, one team owns everything.

Pattern 2: Nested Modules (Medium/Large Projects)

infrastructure/
├── environments/
│   ├── dev/
│   │   ├── main.tf          # Calls modules with dev params
│   │   ├── backend.tf       # Dev state backend
│   │   └── terraform.tfvars
│   ├── staging/
│   │   └── ...
│   └── prod/
│       └── ...
├── modules/
│   ├── networking/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── outputs.tf
│   ├── compute/
│   │   └── ...
│   └── database/
│       └── ...
└── versions.tf

Best for: Multiple environments, shared infrastructure patterns, team collaboration.

Pattern 3: Mono-Repo with Terragrunt

infrastructure/
├── terragrunt.hcl           # Root config
├── modules/                  # Reusable modules
│   ├── vpc/
│   ├── eks/
│   └── rds/
├── dev/
│   ├── terragrunt.hcl       # Dev overrides
│   ├── vpc/
│   │   └── terragrunt.hcl   # Module invocation
│   └── eks/
│       └── terragrunt.hcl
└── prod/
    ├── terragrunt.hcl
    └── ...

Best for: Large-scale, many environments, DRY configuration, team-level isolation.


Provider Configuration Patterns

Version Pinning

terraform {
  required_version = ">= 1.5.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"    # Allow 5.x, block 6.0
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.5"
    }
  }
}

Multi-Region with Aliases

provider "aws" {
  region = "us-east-1"
}

provider "aws" {
  alias  = "west"
  region = "us-west-2"
}

resource "aws_s3_bucket" "primary" {
  bucket = "my-app-primary"
}

resource "aws_s3_bucket" "replica" {
  provider = aws.west
  bucket   = "my-app-replica"
}

Multi-Account with Assume Role

provider "aws" {
  alias  = "production"
  region = "us-east-1"

  assume_role {
    role_arn = "arn:aws:iam::PROD_ACCOUNT_ID:role/TerraformRole"
  }
}

State Management Decision Tree

Single developer, small project?
├── Yes → Local state (but migrate to remote ASAP)
└── No
    ├── Using Terraform Cloud/Enterprise?
    │   └── Yes → TF Cloud native backend (built-in locking, encryption, RBAC)
    └── No
        ├── AWS?
        │   └── S3 + DynamoDB (encryption, locking, versioning)
        ├── GCP?
        │   └── GCS bucket (native locking, encryption)
        ├── Azure?
        │   └── Azure Blob Storage (native locking, encryption)
        └── Other?
            └── Consul or PostgreSQL backend

Environment isolation strategy:
├── Separate state files per environment (recommended)
│   ├── Option A: Separate directories (dev/, staging/, prod/)
│   └── Option B: Terraform workspaces (simpler but less isolation)
└── Single state file for all environments (never do this)

CI/CD Integration Patterns

GitHub Actions Plan/Apply

# .github/workflows/terraform.yml
name: Terraform
on:
  pull_request:
    paths: ['terraform/**']
  push:
    branches: [main]
    paths: ['terraform/**']

jobs:
  plan:
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
      - run: terraform init
      - run: terraform validate
      - run: terraform plan -out=tfplan
      - run: terraform show -json tfplan > plan.json
      # Post plan as PR comment

  apply:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    environment: production
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
      - run: terraform init
      - run: terraform apply -auto-approve

Drift Detection

# Run on schedule to detect drift
name: Drift Detection
on:
  schedule:
    - cron: '0 6 * * 1-5'  # Weekdays at 6 AM

jobs:
  detect:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
      - run: terraform init
      - run: |
          terraform plan -detailed-exitcode -out=drift.tfplan 2>&1 | tee drift.log
          EXIT_CODE=$?
          if [ $EXIT_CODE -eq 2 ]; then
            echo "DRIFT DETECTED — review drift.log"
            # Send alert (Slack, PagerDuty, etc.)
          fi

Proactive Triggers

Flag these without being asked:

  • No remote backend configured → Migrate to S3/GCS/Azure Blob with locking and encryption.
  • Provider without version constraint → Add version = "~> X.0" to prevent breaking upgrades.
  • Hardcoded secrets in .tf files → Use variables with sensitive = true, or integrate Vault/SSM.
  • *IAM policy with `"Action": ""`** → Scope to specific actions. No wildcard actions in production.
  • Security group open to 0.0.0.0/0 on SSH/RDP → Restrict to bastion CIDR or use SSM Session Manager.
  • No state locking → Enable DynamoDB table for S3 backend, or use TF Cloud.
  • Resources without tags → Add default_tags in provider block. Tags are mandatory for cost tracking.
  • Missing prevent_destroy on databases/storage → Add lifecycle block to prevent accidental deletion.

Installation

One-liner (any tool)

git clone https://github.com/alirezarezvani/claude-skills.git
cp -r claude-skills/engineering/terraform-patterns ~/.claude/skills/

Multi-tool install

./scripts/convert.sh --skill terraform-patterns --tool codex|gemini|cursor|windsurf|openclaw

OpenClaw

clawhub install terraform-patterns

Related Skills

  • senior-devops — Broader DevOps scope (CI/CD, monitoring, containerization). Complementary — use terraform-patterns for IaC-specific work, senior-devops for pipeline and infrastructure operations.
  • aws-solution-architect — AWS architecture design. Complementary — terraform-patterns implements the infrastructure, aws-solution-architect designs it.
  • senior-security — Application security. Complementary — terraform-patterns covers infrastructure security posture, senior-security covers application-level threats.
  • ci-cd-pipeline-builder — Pipeline construction. Complementary — terraform-patterns defines infrastructure, ci-cd-pipeline-builder automates deployment.

同梱ファイル

※ ZIPに含まれるファイル一覧。`SKILL.md` 本体に加え、参考資料・サンプル・スクリプトが入っている場合があります。