terraform

MUST USE when creating, reviewing, testing, or refactoring Terraform code. Covers modules, providers, resources, variables, CI/CD workflows, policy and validation patterns, and infrastructure-as-code design decisions. Do NOT use for imperative cloud CLI tasks or non-Terraform deployment tooling unless the task explicitly requires Terraform.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "terraform" with this command: npx skills add benjaminwestern/google-engineer-skills/benjaminwestern-google-engineer-skills-terraform

Terraform Skill

Comprehensive Terraform guidance covering modules, testing, CI/CD, and production patterns. Combines best practices from terraform-best-practices.com and Cloud Foundation Fabric.


The Seven Mantras

These principles guide all Terraform work:

  1. Always Have versions.tf — Every root module must define providers, versions, and configuration. Never leave provider config implicit.
  2. Always Have backend.tf — Even if empty initially, you'll need remote state (GCS for GCP).
  3. Always Have locals.tf — Centralize all local values, don't scatter them across files.
  4. Vertical Files by Domain — Organize by workload (webserver.tf, salesforce-etl.tf), not by resource type (iam.tf, compute.tf).
  5. Variables with Sane Defaults — Provide sensible defaults and example tfvars files.
  6. YAML Factories for Heavy Reuse — Enable non-Terraform users via YAML configs + factory patterns.
  7. Maximize Autonomy — App repos own their resources, foundations repos own shared infrastructure. Avoid cross-repo dependencies.

Core Principles

1. Required Files

Every Terraform root module must have:

FilePurpose
versions.tfProvider versions and configuration
backend.tfRemote state (even if empty initially)
locals.tfCentralized local values
variables.tfInput variables (when needed)

2. Vertical File Organization

Organize by domain/workload, not by resource type:

# GOOD - Files by domain:
├── networking.tf      # VPC, subnets, routes, NAT
├── webserver.tf       # MIG, template, LB, service account
├── salesforce-etl.tf  # SA, secrets, Cloud Function, BigQuery
└── database.tf        # Cloud SQL, IAM, backups

# BAD - Files by resource type:
├── iam.tf            # All IAM mixed together
├── compute.tf        # All compute mixed together
└── storage.tf        # All storage mixed together

3. Naming Conventions

TypePatternExample
ResourcesDescriptive, contextualweb_server, application_logs
SingletonsUse thisgoogle_compute_network.this
VariablesContext-specificvpc_cidr_block, database_instance_class
Outputs{name}_{type}_{attribute}firewall_rule_id, subnet_ids

4. Module Boundaries

TypeScopeExample
Resource ModuleSingle logical groupVPC + subnets
Infrastructure ModuleCollection of resourcesComplete networking stack
CompositionEnvironment-specificProduction environment
RepositoryOwns its resourcesApp repo = app resources only

Quick Decision Reference

Count vs For_Each

Do resources need meaningful identifiers?
│
├─ YES → for_each (with set or map)
│
└─ NO → Can items change position?
    ├─ YES → for_each (prevents cascade)
    └─ NO → count (simpler)

See code-patterns.md for migration patterns and detailed examples.

Testing Approach

What do you need to test?
│
├─ Syntax/format? → terraform validate + fmt
├─ Security? → trivy + checkov
├─ Simple logic (1.6+)? → Native tests
└─ Complex integration? → Terratest

See testing-frameworks.md for implementation details.

Authentication (GCP)

Always use OIDC. Never use service account keys.

MethodSecurityUse Case
OIDC✅ No long-lived credentialsCI/CD (GitHub Actions)
ADC✅ User credentialsLocal development
Impersonation✅ Service account delegationAutomation
Keys❌ Long-lived riskNever use

See security-compliance.md for OIDC setup and security patterns.


Modern Features (Quick Reference)

FeatureVersionWhen to Use
try()0.13+Safe fallbacks (always use)
optional()1.3+Optional object attributes
moved blocks1.1+Refactor without recreation
Native tests1.6+Unit testing in HCL
Mock providers1.7+Cost-free testing
Write-only args1.11+Secrets (never in state)

Essential Commands

# Static analysis (always free)
terraform fmt -recursive -check
terraform validate
tflint
trivy config .
checkov -d .

# Testing (1.6+)
terraform test

# Plan and apply
terraform plan -out=tfplan
terraform apply -auto-approve tfplan

# State management
terraform state list
terraform show

See quick-reference.md for full cheat sheet.


Security Checklist

  • Authentication: Using OIDC (not keys)
  • Secrets: In Secret Manager (not state/variables)
  • Network: Custom VPC (not default)
  • Firewall: Least privilege (not 0.0.0.0/0)
  • State: Encrypted, versioned, restricted access
  • Scanning: Trivy/Checkov in CI/CD

See security-compliance.md for detailed patterns.


Module Checklist

  • versions.tf with provider constraints
  • backend.tf for remote state
  • variables.tf with descriptions and validation
  • outputs.tf with descriptions
  • examples/ directory (simple and complete)
  • Tests (native 1.6+ or Terratest)
  • Pre-commit hooks configured
  • Documentation in README.md

See module-patterns.md for details.


CI/CD Essentials

Standard Pipeline

validate → plan → apply

Key Decisions

DecisionRecommendation
AuthenticationOIDC (never keys)
Apply triggerPush for dev/staging, approval for prod
WorkspacesSeparate files per environment
Security scanTrivy + Checkov in CI

See ci-cd-workflows.md for templates.


Reference Documentation

code-patterns.md

When to use: Writing or refactoring Terraform code

Covers:

  • Count vs for_each decision guide and migration patterns
  • Block ordering rules (resources, variables)
  • Modern features decision guide (try, optional, moved, write-only)
  • Version constraints and update strategy
  • Secrets management patterns
  • Refactoring from legacy (0.12/0.13) to modern syntax

quick-reference.md

When to use: Need quick lookup or troubleshooting

Covers:

  • Command cheat sheet (format, validate, test, plan)
  • Decision flowcharts (testing, module workflow, refactoring)
  • Version-specific guidance (1.0-1.5, 1.6+, 1.7+)
  • Troubleshooting common issues
  • Version constraint syntax reference

module-patterns.md

When to use: Creating or restructuring modules

Covers:

  • Module type decision tree (Resource → Infrastructure → Composition)
  • Architecture decisions (scope size, module connections)
  • File organization standards
  • Parameterization vs hardcoding
  • Root module vs reusable module boundaries
  • Naming decisions (variables, outputs)
  • Anti-patterns to avoid (god modules, environment sprawl)

testing-frameworks.md

When to use: Setting up or choosing testing approach

Covers:

  • Testing decision flowchart
  • Native tests vs Terratest comparison
  • Critical plan vs apply decision
  • Working with set-type blocks
  • Mocking decisions
  • Terratest patterns and cost management
  • Testing checklist

ci-cd-workflows.md

When to use: Setting up CI/CD pipelines

Covers:

  • Standard validate → plan → apply pipeline
  • OIDC authentication patterns
  • Apply strategy options (push, approval, comment-triggered)
  • Environment organization (separate vs reusable workflows)
  • Essential security checks in CI
  • Atlantis integration decision guide

security-compliance.md

When to use: Security review or compliance setup

Covers:

  • OIDC vs service account key decision
  • Secrets management (what's safe vs unsafe)
  • Network architecture decisions
  • Firewall rule best practices
  • State security requirements
  • Compliance testing tools (trivy, checkov, terraform-compliance, OPA)

factory-patterns.md

When to use: Building scalable, self-service infrastructure

Covers:

  • When to use factory vs traditional modules
  • Configuration format decisions (YAML vs JSON)
  • Discovery methods (single file vs auto-discovery)
  • Default strategy patterns
  • Factory patterns by use case (Project, Subnet, Service Account)
  • Conditional resource creation
  • Lifecycle hooks
  • When NOT to use factory pattern

Based on:

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

playwright-cli

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

opencode-dev

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

github-profile-architect

No summary provided by upstream source.

Repository SourceNeeds Review
General

duckdb

No summary provided by upstream source.

Repository SourceNeeds Review