jpskill.com
🛠️ 開発・MCP コミュニティ 🔴 エンジニア向け 👤 エンジニア・AI開発者

🛠️ Terraform Skill

terraform-skill

Terraform を活用し、インフラ構築のベストプラクティスに沿ったコードを効率的に管理するSkill。

⏱ コードレビュー 1時間 → 10分

📺 まず動画で見る(YouTube)

▶ 【衝撃】最強のAIエージェント「Claude Code」の最新機能・使い方・プログラミングをAIで効率化する超実践術を解説! ↗

※ jpskill.com 編集部が参考用に選んだ動画です。動画の内容と Skill の挙動は厳密には一致しないことがあります。

📜 元の英語説明(参考)

Terraform infrastructure as code best practices

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

一言でいうと

Terraform を活用し、インフラ構築のベストプラクティスに沿ったコードを効率的に管理するSkill。

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

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

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

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

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

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して terraform-skill.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → terraform-skill フォルダができる
  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-17
同梱ファイル
1

💬 こう話しかけるだけ — サンプルプロンプト

  • Terraform Skill を使って、最小構成のサンプルコードを示して
  • Terraform Skill の主な使い方と注意点を教えて
  • Terraform Skill を既存プロジェクトに組み込む方法を教えて

これをClaude Code に貼るだけで、このSkillが自動発動します。

📖 Skill本文(日本語訳)

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

[スキル名] terraform-skill

Claude向けTerraformスキル

テスト、モジュール、CI/CD、および本番環境パターンを網羅した、TerraformとOpenTofuに関する包括的なガイダンスを提供します。terraform-best-practices.comとエンタープライズでの経験に基づいています。

このスキルを使用するタイミング

以下の場合は、このスキルをアクティブにしてください。

  • 新しいTerraformまたはOpenTofuの設定やモジュールを作成する場合
  • IaCコードのテストインフラストラクチャをセットアップする場合
  • テストアプローチ(validate、plan、フレームワーク)を決定する場合
  • マルチ環境デプロイメントを構築する場合
  • Infrastructure-as-CodeのCI/CDを実装する場合
  • 既存のTerraform/OpenTofuプロジェクトをレビューまたはリファクタリングする場合
  • モジュールパターンまたは状態管理アプローチを選択する場合

以下の場合は、このスキルを使用しないでください。

  • 基本的なTerraform/OpenTofuの構文に関する質問(Claudeはこれを理解しています)
  • プロバイダー固有のAPIリファレンス(代わりにドキュメントへのリンクを使用してください)
  • Terraform/OpenTofuと関係のないクラウドプラットフォームに関する質問

主要な原則

1. コード構造の哲学

モジュール階層:

タイプ 使用するタイミング スコープ
リソースモジュール 接続されたリソースの単一の論理グループ VPC + サブネット、セキュリティグループ + ルール
インフラストラクチャモジュール 特定の目的のためのリソースモジュールの集合 1つのリージョン/アカウント内の複数のリソースモジュール
コンポジション 完全なインフラストラクチャ 複数のリージョン/アカウントにまたがる

階層: リソース → リソースモジュール → インフラストラクチャモジュール → コンポジション

ディレクトリ構造:

environments/        # 環境固有の設定
├── prod/
├── staging/
└── dev/

modules/            # 再利用可能なモジュール
├── networking/
├── compute/
└── data/

examples/           # モジュールの使用例(テストとしても機能)
├── complete/
└── minimal/

terraform-best-practices.comからの主要な原則:

  • 環境(prod、staging)とモジュール(再利用可能なコンポーネント)を分離する
  • examples/ をドキュメントと統合テストフィクスチャの両方として使用する
  • モジュールは小さく、焦点を絞る(単一責任)

詳細なモジュールアーキテクチャについては、以下を参照してください。 Code Patterns: Module Types & Hierarchy

2. 命名規則

リソース:

# 良い例: 説明的で文脈的
resource "aws_instance" "web_server" { }
resource "aws_s3_bucket" "application_logs" { }

# 良い例: シングルトンリソース(そのタイプが1つだけ)には "this" を使用
resource "aws_vpc" "this" { }
resource "aws_security_group" "this" { }

# 避けるべき例: シングルトンではないものに一般的な名前を使用
resource "aws_instance" "main" { }
resource "aws_s3_bucket" "bucket" { }

シングルトンリソース:

モジュールがそのタイプのリソースを1つだけ作成する場合に "this" を使用します。

✅ 良い例:

resource "aws_vpc" "this" {}           # モジュールは1つのVPCを作成
resource "aws_security_group" "this" {}  # モジュールは1つのSGを作成

❌ 複数のリソースには "this" を使用しないでください:

resource "aws_subnet" "this" {}  # 複数のサブネットを作成する場合

同じタイプのリソースを複数作成する場合は、説明的な名前を使用してください。

変数:

# 必要に応じてコンテキストをプレフィックスとして追加
var.vpc_cidr_block          # 単に "cidr" ではない
var.database_instance_class # 単に "instance_class" ではない

ファイル:

  • main.tf - 主要なリソース
  • variables.tf - 入力変数
  • outputs.tf - 出力値
  • versions.tf - プロバイダーバージョン
  • data.tf - データソース(オプション)

テスト戦略フレームワーク

決定マトリックス: どのテストアプローチを選択するか?

あなたの状況 推奨されるアプローチ ツール コスト
簡単な構文チェック 静的解析 terraform validatefmt 無料
コミット前検証 静的 + リント validatetflinttrivycheckov 無料
Terraform 1.6+、シンプルなロジック ネイティブテストフレームワーク 組み込みの terraform test 無料〜低
1.6以前、またはGoの専門知識 統合テスト Terratest 低〜中
セキュリティ/コンプライアンス重視 Policy as code OPA、Sentinel 無料
コスト重視のワークフロー モックプロバイダー (1.7+) ネイティブテスト + モック 無料
マルチクラウド、複雑 完全な統合 Terratest + 実際のインフラ 中〜高

インフラストラクチャのテストピラミッド

        /\
       /  \          エンドツーエンドテスト (高コスト)
      /____\         - 完全な環境デプロイメント
     /      \        - 本番環境に近いセットアップ
    /________\
   /          \      統合テスト (中程度)
  /____________\     - モジュールの単体テスト
 /              \    - テストアカウント内の実際のリソース
/________________\   静的解析 (低コスト)
                     - validate, fmt, lint
                     - セキュリティスキャン

ネイティブテストのベストプラクティス (1.6+)

テストコードを生成する前に:

  1. Terraform MCPでスキーマを検証する:

    プロバイダーのドキュメントを検索 → リソーススキーマを取得 → ブロックタイプを特定
  2. 正しいコマンドモードを選択する:

    • command = plan - 高速、入力検証用
    • command = apply - 計算された値とセット型ブロックに必要
  3. セット型ブロックを正しく処理する:

    • [0] でインデックスを付けることはできない
    • for 式を使用して反復する
    • または command = apply を使用して具体化する

一般的なパターン:

  • S3暗号化ルール: セット(for式を使用)
  • ライフサイクルトランジション: セット(for式を使用)
  • IAMポリシーステートメント: セット(for式を使用)

詳細なテストガイドについては、以下を参照してください。

  • Testing Frameworks Guide - 静的解析、ネイティブテスト、Terratestの詳細
  • Quick Reference - 決定フローチャートとコマンドチートシート

コード構造標準

リソースブロックの順序

一貫性のための厳密な順序:

  1. count または for_each が最初(その後に空行)
  2. その他の引数
  3. tags が最後の実際の引数
  4. depends_on がタグの後(必要な場合)
  5. lifecycle が一番最後(必要な場合)
# ✅ 良い例 - 正しい順序
resource "aws_nat_gateway" "this" {
  count = var.create_nat_gateway ? 1 : 0

  allocation_id = aws_eip.this[0].id
  subnet_id     = aws_subnet.public[0].id

  tags = {
    Name = "${var.name}-nat"
  }

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

Terraform Skill for Claude

Comprehensive Terraform and OpenTofu guidance covering testing, modules, CI/CD, and production patterns. Based on terraform-best-practices.com and enterprise experience.

When to Use This Skill

Activate this skill when:

  • Creating new Terraform or OpenTofu configurations or modules
  • Setting up testing infrastructure for IaC code
  • Deciding between testing approaches (validate, plan, frameworks)
  • Structuring multi-environment deployments
  • Implementing CI/CD for infrastructure-as-code
  • Reviewing or refactoring existing Terraform/OpenTofu projects
  • Choosing between module patterns or state management approaches

Don't use this skill for:

  • Basic Terraform/OpenTofu syntax questions (Claude knows this)
  • Provider-specific API reference (link to docs instead)
  • Cloud platform questions unrelated to Terraform/OpenTofu

Core Principles

1. Code Structure Philosophy

Module Hierarchy:

Type When to Use Scope
Resource Module Single logical group of connected resources VPC + subnets, Security group + rules
Infrastructure Module Collection of resource modules for a purpose Multiple resource modules in one region/account
Composition Complete infrastructure Spans multiple regions/accounts

Hierarchy: Resource → Resource Module → Infrastructure Module → Composition

Directory Structure:

environments/        # Environment-specific configurations
├── prod/
├── staging/
└── dev/

modules/            # Reusable modules
├── networking/
├── compute/
└── data/

examples/           # Module usage examples (also serve as tests)
├── complete/
└── minimal/

Key principle from terraform-best-practices.com:

  • Separate environments (prod, staging) from modules (reusable components)
  • Use examples/ as both documentation and integration test fixtures
  • Keep modules small and focused (single responsibility)

For detailed module architecture, see: Code Patterns: Module Types & Hierarchy

2. Naming Conventions

Resources:

# Good: Descriptive, contextual
resource "aws_instance" "web_server" { }
resource "aws_s3_bucket" "application_logs" { }

# Good: "this" for singleton resources (only one of that type)
resource "aws_vpc" "this" { }
resource "aws_security_group" "this" { }

# Avoid: Generic names for non-singletons
resource "aws_instance" "main" { }
resource "aws_s3_bucket" "bucket" { }

Singleton Resources:

Use "this" when your module creates only one resource of that type:

✅ DO:

resource "aws_vpc" "this" {}           # Module creates one VPC
resource "aws_security_group" "this" {}  # Module creates one SG

❌ DON'T use "this" for multiple resources:

resource "aws_subnet" "this" {}  # If creating multiple subnets

Use descriptive names when creating multiple resources of the same type.

Variables:

# Prefix with context when needed
var.vpc_cidr_block          # Not just "cidr"
var.database_instance_class # Not just "instance_class"

Files:

  • main.tf - Primary resources
  • variables.tf - Input variables
  • outputs.tf - Output values
  • versions.tf - Provider versions
  • data.tf - Data sources (optional)

Testing Strategy Framework

Decision Matrix: Which Testing Approach?

Your Situation Recommended Approach Tools Cost
Quick syntax check Static analysis terraform validate, fmt Free
Pre-commit validation Static + lint validate, tflint, trivy, checkov Free
Terraform 1.6+, simple logic Native test framework Built-in terraform test Free-Low
Pre-1.6, or Go expertise Integration testing Terratest Low-Med
Security/compliance focus Policy as code OPA, Sentinel Free
Cost-sensitive workflow Mock providers (1.7+) Native tests + mocking Free
Multi-cloud, complex Full integration Terratest + real infra Med-High

Testing Pyramid for Infrastructure

        /\
       /  \          End-to-End Tests (Expensive)
      /____\         - Full environment deployment
     /      \        - Production-like setup
    /________\
   /          \      Integration Tests (Moderate)
  /____________\     - Module testing in isolation
 /              \    - Real resources in test account
/________________\   Static Analysis (Cheap)
                     - validate, fmt, lint
                     - Security scanning

Native Test Best Practices (1.6+)

Before generating test code:

  1. Validate schemas with Terraform MCP:

    Search provider docs → Get resource schema → Identify block types
  2. Choose correct command mode:

    • command = plan - Fast, for input validation
    • command = apply - Required for computed values and set-type blocks
  3. Handle set-type blocks correctly:

    • Cannot index with [0]
    • Use for expressions to iterate
    • Or use command = apply to materialize

Common patterns:

  • S3 encryption rules: set (use for expressions)
  • Lifecycle transitions: set (use for expressions)
  • IAM policy statements: set (use for expressions)

For detailed testing guides, see:

  • Testing Frameworks Guide - Deep dive into static analysis, native tests, and Terratest
  • Quick Reference - Decision flowchart and command cheat sheet

Code Structure Standards

Resource Block Ordering

Strict ordering for consistency:

  1. count or for_each FIRST (blank line after)
  2. Other arguments
  3. tags as last real argument
  4. depends_on after tags (if needed)
  5. lifecycle at the very end (if needed)
# ✅ GOOD - Correct ordering
resource "aws_nat_gateway" "this" {
  count = var.create_nat_gateway ? 1 : 0

  allocation_id = aws_eip.this[0].id
  subnet_id     = aws_subnet.public[0].id

  tags = {
    Name = "${var.name}-nat"
  }

  depends_on = [aws_internet_gateway.this]

  lifecycle {
    create_before_destroy = true
  }
}

Variable Block Ordering

  1. description (ALWAYS required)
  2. type
  3. default
  4. validation
  5. nullable (when setting to false)
variable "environment" {
  description = "Environment name for resource tagging"
  type        = string
  default     = "dev"

  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be one of: dev, staging, prod."
  }

  nullable = false
}

For complete structure guidelines, see: Code Patterns: Block Ordering & Structure

Count vs For_Each: When to Use Each

Quick Decision Guide

Scenario Use Why
Boolean condition (create or don't) count = condition ? 1 : 0 Simple on/off toggle
Simple numeric replication count = 3 Fixed number of identical resources
Items may be reordered/removed for_each = toset(list) Stable resource addresses
Reference by key for_each = map Named access to resources
Multiple named resources for_each Better maintainability

Common Patterns

Boolean conditions:

# ✅ GOOD - Boolean condition
resource "aws_nat_gateway" "this" {
  count = var.create_nat_gateway ? 1 : 0
  # ...
}

Stable addressing with for_each:

# ✅ GOOD - Removing "us-east-1b" only affects that subnet
resource "aws_subnet" "private" {
  for_each = toset(var.availability_zones)

  availability_zone = each.key
  # ...
}

# ❌ BAD - Removing middle AZ recreates all subsequent subnets
resource "aws_subnet" "private" {
  count = length(var.availability_zones)

  availability_zone = var.availability_zones[count.index]
  # ...
}

For migration guides and detailed examples, see: Code Patterns: Count vs For_Each

Locals for Dependency Management

Use locals to ensure correct resource deletion order:

# Problem: Subnets might be deleted after CIDR blocks, causing errors
# Solution: Use try() in locals to hint deletion order

locals {
  # References secondary CIDR first, falling back to VPC
  # Forces Terraform to delete subnets before CIDR association
  vpc_id = try(
    aws_vpc_ipv4_cidr_block_association.this[0].vpc_id,
    aws_vpc.this.id,
    ""
  )
}

resource "aws_vpc" "this" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_vpc_ipv4_cidr_block_association" "this" {
  count = var.add_secondary_cidr ? 1 : 0

  vpc_id     = aws_vpc.this.id
  cidr_block = "10.1.0.0/16"
}

resource "aws_subnet" "public" {
  vpc_id     = local.vpc_id  # Uses local, not direct reference
  cidr_block = "10.1.0.0/24"
}

Why this matters:

  • Prevents deletion errors when destroying infrastructure
  • Ensures correct dependency order without explicit depends_on
  • Particularly useful for VPC configurations with secondary CIDR blocks

For detailed examples, see: Code Patterns: Locals for Dependency Management

Module Development

Standard Module Structure

my-module/
├── README.md           # Usage documentation
├── main.tf             # Primary resources
├── variables.tf        # Input variables with descriptions
├── outputs.tf          # Output values
├── versions.tf         # Provider version constraints
├── examples/
│   ├── minimal/        # Minimal working example
│   └── complete/       # Full-featured example
└── tests/              # Test files
    └── module_test.tftest.hcl  # Or .go

Best Practices Summary

Variables:

  • ✅ Always include description
  • ✅ Use explicit type constraints
  • ✅ Provide sensible default values where appropriate
  • ✅ Add validation blocks for complex constraints
  • ✅ Use sensitive = true for secrets

Outputs:

  • ✅ Always include description
  • ✅ Mark sensitive outputs with sensitive = true
  • ✅ Consider returning objects for related values
  • ✅ Document what consumers should do with each output

For detailed module patterns, see:

  • Module Patterns Guide - Variable best practices, output design, ✅ DO vs ❌ DON'T patterns
  • Quick Reference - Resource naming, variable naming, file organization

CI/CD Integration

Recommended Workflow Stages

  1. Validate - Format check + syntax validation + linting
  2. Test - Run automated tests (native or Terratest)
  3. Plan - Generate and review execution plan
  4. Apply - Execute changes (with approvals for production)

Cost Optimization Strategy

  1. Use mocking for PR validation (free)
  2. Run integration tests only on main branch (controlled cost)
  3. Implement auto-cleanup (prevent orphaned resources)
  4. Tag all test resources (track spending)

For complete CI/CD templates, see:

  • CI/CD Workflows Guide - GitHub Actions, GitLab CI, Atlantis integration, cost optimization
  • Quick Reference - Common CI/CD issues and solutions

Security & Compliance

Essential Security Checks

# Static security scanning
trivy config .
checkov -d .

Common Issues to Avoid

Don't:

  • Store secrets in variables
  • Use default VPC
  • Skip encryption
  • Open security groups to 0.0.0.0/0

Do:

  • Use AWS Secrets Manager / Parameter Store
  • Create dedicated VPCs
  • Enable encryption at rest
  • Use least-privilege security groups

For detailed security guidance, see:

  • Security & Compliance Guide - Trivy/Checkov integration, secrets management, state file security, compliance testing

Version Management

Version Constraint Syntax

version = "5.0.0"      # Exact (avoid - inflexible)
version = "~> 5.0"     # Recommended: 5.0.x only
version = ">= 5.0"     # Minimum (risky - breaking changes)

Strategy by Component

Component Strategy Example
Terraform Pin minor version required_version = "~> 1.9"
Providers Pin major version version = "~> 5.0"
Modules (prod) Pin exact version version = "5.1.2"
Modules (dev) Allow patch updates version = "~> 5.1"

Update Workflow

# Lock versions initially
terraform init              # Creates .terraform.lock.hcl

# Update to latest within constraints
terraform init -upgrade     # Updates providers

# Review and test
terraform plan

For detailed version management, see: Code Patterns: Version Management

Modern Terraform Features (1.0+)

Feature Availability by Version

Feature Version Use Case
try() function 0.13+ Safe fallbacks, replaces element(concat())
nullable = false 1.1+ Prevent null values in variables
moved blocks 1.1+ Refactor without destroy/recreate
optional() with defaults 1.3+ Optional object attributes
Native testing 1.6+ Built-in test framework
Mock providers 1.7+ Cost-free unit testing
Provider functions 1.8+ Provider-specific data transformation
Cross-variable validation 1.9+ Validate relationships between variables
Write-only arguments 1.11+ Secrets never stored in state

Quick Examples

# try() - Safe fallbacks (0.13+)
output "sg_id" {
  value = try(aws_security_group.this[0].id, "")
}

# optional() - Optional attributes with defaults (1.3+)
variable "config" {
  type = object({
    name    = string
    timeout = optional(number, 300)  # Default: 300
  })
}

# Cross-variable validation (1.9+)
variable "environment" { type = string }
variable "backup_days" {
  type = number
  validation {
    condition     = var.environment == "prod" ? var.backup_days >= 7 : true
    error_message = "Production requires backup_days >= 7"
  }
}

For complete patterns and examples, see: Code Patterns: Modern Terraform Features

Version-Specific Guidance

Terraform 1.0-1.5

  • Use Terratest for testing
  • No native testing framework available
  • Focus on static analysis and plan validation

Terraform 1.6+ / OpenTofu 1.6+

  • New: Native terraform test / tofu test command
  • Consider migrating from external frameworks for simple tests
  • Keep Terratest only for complex integration tests

Terraform 1.7+ / OpenTofu 1.7+

  • New: Mock providers for unit testing
  • Reduce cost by mocking external dependencies
  • Use real integration tests for final validation

Terraform vs OpenTofu

Both are fully supported by this skill. For licensing, governance, and feature comparison, see Quick Reference: Terraform vs OpenTofu.

Detailed Guides

This skill uses progressive disclosure - essential information is in this main file, detailed guides are available when needed:

📚 Reference Files:

  • Testing Frameworks - In-depth guide to static analysis, native tests, and Terratest
  • Module Patterns - Module structure, variable/output best practices, ✅ DO vs ❌ DON'T patterns
  • CI/CD Workflows - GitHub Actions, GitLab CI templates, cost optimization, automated cleanup
  • Security & Compliance - Trivy/Checkov integration, secrets management, compliance testing
  • Quick Reference - Command cheat sheets, decision flowcharts, troubleshooting guide

How to use: When you need detailed information on a topic, reference the appropriate guide. Claude will load it on demand to provide comprehensive guidance.

License

This skill is licensed under the Apache License 2.0. See the LICENSE file for full terms.

Copyright © 2026 Anton Babenko

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.