$npx -y skills add agentscope-ai/QwenPaw --skill terraform-skillUse when working with Terraform or OpenTofu - creating modules, writing tests (native test framework, Terratest), setting up CI/CD pipelines, reviewing configurations, choosing between testing approaches, debugging state issues, implementing security scanning (trivy, checkov), or
| 1 | # Terraform Skill for Claude |
| 2 | |
| 3 | Comprehensive Terraform and OpenTofu guidance covering testing, modules, CI/CD, and production patterns. Based on terraform-best-practices.com and enterprise experience. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | **Activate this skill when:** |
| 8 | - Creating new Terraform or OpenTofu configurations or modules |
| 9 | - Setting up testing infrastructure for IaC code |
| 10 | - Deciding between testing approaches (validate, plan, frameworks) |
| 11 | - Structuring multi-environment deployments |
| 12 | - Implementing CI/CD for infrastructure-as-code |
| 13 | - Reviewing or refactoring existing Terraform/OpenTofu projects |
| 14 | - Choosing between module patterns or state management approaches |
| 15 | |
| 16 | **Don't use this skill for:** |
| 17 | - Basic Terraform/OpenTofu syntax questions (Claude knows this) |
| 18 | - Provider-specific API reference (link to docs instead) |
| 19 | - Cloud platform questions unrelated to Terraform/OpenTofu |
| 20 | |
| 21 | ## Core Principles |
| 22 | |
| 23 | ### 1. Code Structure Philosophy |
| 24 | |
| 25 | **Module Hierarchy:** |
| 26 | |
| 27 | | Type | When to Use | Scope | |
| 28 | |------|-------------|-------| |
| 29 | | **Resource Module** | Single logical group of connected resources | VPC + subnets, Security group + rules | |
| 30 | | **Infrastructure Module** | Collection of resource modules for a purpose | Multiple resource modules in one region/account | |
| 31 | | **Composition** | Complete infrastructure | Spans multiple regions/accounts | |
| 32 | |
| 33 | **Hierarchy:** Resource → Resource Module → Infrastructure Module → Composition |
| 34 | |
| 35 | **Directory Structure:** |
| 36 | ``` |
| 37 | environments/ # Environment-specific configurations |
| 38 | ├── prod/ |
| 39 | ├── staging/ |
| 40 | └── dev/ |
| 41 | |
| 42 | modules/ # Reusable modules |
| 43 | ├── networking/ |
| 44 | ├── compute/ |
| 45 | └── data/ |
| 46 | |
| 47 | examples/ # Module usage examples (also serve as tests) |
| 48 | ├── complete/ |
| 49 | └── minimal/ |
| 50 | ``` |
| 51 | |
| 52 | **Key principle from terraform-best-practices.com:** |
| 53 | - Separate **environments** (prod, staging) from **modules** (reusable components) |
| 54 | - Use **examples/** as both documentation and integration test fixtures |
| 55 | - Keep modules small and focused (single responsibility) |
| 56 | |
| 57 | **For detailed module architecture, see:** [Code Patterns: Module Types & Hierarchy](references/code-patterns.md) |
| 58 | |
| 59 | ### 2. Naming Conventions |
| 60 | |
| 61 | **Resources:** |
| 62 | ```hcl |
| 63 | # Good: Descriptive, contextual |
| 64 | resource "aws_instance" "web_server" { } |
| 65 | resource "aws_s3_bucket" "application_logs" { } |
| 66 | |
| 67 | # Good: "this" for singleton resources (only one of that type) |
| 68 | resource "aws_vpc" "this" { } |
| 69 | resource "aws_security_group" "this" { } |
| 70 | |
| 71 | # Avoid: Generic names for non-singletons |
| 72 | resource "aws_instance" "main" { } |
| 73 | resource "aws_s3_bucket" "bucket" { } |
| 74 | ``` |
| 75 | |
| 76 | **Singleton Resources:** |
| 77 | |
| 78 | Use `"this"` when your module creates only one resource of that type: |
| 79 | |
| 80 | ✅ DO: |
| 81 | ```hcl |
| 82 | resource "aws_vpc" "this" {} # Module creates one VPC |
| 83 | resource "aws_security_group" "this" {} # Module creates one SG |
| 84 | ``` |
| 85 | |
| 86 | ❌ DON'T use "this" for multiple resources: |
| 87 | ```hcl |
| 88 | resource "aws_subnet" "this" {} # If creating multiple subnets |
| 89 | ``` |
| 90 | |
| 91 | Use descriptive names when creating multiple resources of the same type. |
| 92 | |
| 93 | **Variables:** |
| 94 | ```hcl |
| 95 | # Prefix with context when needed |
| 96 | var.vpc_cidr_block # Not just "cidr" |
| 97 | var.database_instance_class # Not just "instance_class" |
| 98 | ``` |
| 99 | |
| 100 | **Files:** |
| 101 | - `main.tf` - Primary resources |
| 102 | - `variables.tf` - Input variables |
| 103 | - `outputs.tf` - Output values |
| 104 | - `versions.tf` - Provider versions |
| 105 | - `data.tf` - Data sources (optional) |
| 106 | |
| 107 | ## Testing Strategy Framework |
| 108 | |
| 109 | ### Decision Matrix: Which Testing Approach? |
| 110 | |
| 111 | | Your Situation | Recommended Approach | Tools | Cost | |
| 112 | |----------------|---------------------|-------|------| |
| 113 | | **Quick syntax check** | Static analysis | `terraform validate`, `fmt` | Free | |
| 114 | | **Pre-commit validation** | Static + lint | `validate`, `tflint`, `trivy`, `checkov` | Free | |
| 115 | | **Terraform 1.6+, simple logic** | Native test framework | Built-in `terraform test` | Free-Low | |
| 116 | | **Pre-1.6, or Go expertise** | Integration testing | Terratest | Low-Med | |
| 117 | | **Security/compliance focus** | Policy as code | OPA, Sentinel | Free | |
| 118 | | **Cost-sensitive workflow** | Mock providers (1.7+) | Native tests + mocking | Free | |
| 119 | | **Multi-cloud, complex** | Full integration | Terratest + real infra | Med-High | |
| 120 | |
| 121 | ### Testing Pyramid for Infrastructure |
| 122 | |
| 123 | ``` |
| 124 | /\ |
| 125 | / \ End-to-End Tests (Expensive) |
| 126 | /____\ - Full environment deployment |
| 127 | / \ - Production-like setup |
| 128 | /________\ |
| 129 | / \ Integration Tests (Moderate) |
| 130 | /____________\ - Module testing in isolation |
| 131 | / \ - Real resources in test account |
| 132 | /________________\ Static Analysis (Cheap) |
| 133 | - validate, fmt, lint |