← 返回列表
全部 Skill›工具 Tools›terraform-patterns

terraform-patterns

作者 @affaan-m · 语言 en
Terraform and OpenTofu infrastructure-as-code patterns for project layout, remote state, modules, variables, secrets, safe plan/apply workflows, and CI. Use when writing or reviewing .tf files, structuring modules or environments, configuring backends and state locking, or debugging plan drift, state, and provider errors.
工具 Tools调试工具需海外环境执行命令访问网络待翻译
存储库
最近源活动
2026-11-20 09:13
检测 SKILL.md 语言
en
星标 ★
309,846
分支 Fork
45,351
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

Terraform Patterns

Production-grade Terraform (and OpenTofu) patterns for writing infrastructure code that is reviewable, repeatable, and safe to apply.

When to Activate

  • Writing or reviewing .tf or .tfvars files
  • Structuring a repo into modules and environments
  • Configuring a remote backend, state locking, or state migration
  • Passing variables and handling secrets in infrastructure code
  • Setting up plan/apply automation in CI
  • Debugging a failed plan, state drift, a lock error, or a provider version conflict
  • Refactoring resources without destroying them (moved, import)

When to Use

Same as When to Activate above. This alias satisfies repo skill-format conventions. Use this skill any time you are writing, reviewing, or debugging Terraform or OpenTofu configuration.

How It Works

This skill gives copy-pasteable patterns and a safe command workflow, organized by task:

  1. Project layout - Environments as thin roots that call reusable modules.
  2. Versions and providers - Pin Terraform and provider versions; commit the lock file.
  3. Remote state - Encrypted, locked, per-environment state.
  4. Variables and outputs - Typed, validated inputs and minimal outputs.
  5. Secrets - Keep values out of code, state exposure in mind.
  6. Resources - for_each over count, lifecycle, and tagging.
  7. Modules - Small, single-purpose, versioned.
  8. Refactoring - moved and import blocks instead of destroy/recreate.
  9. Safe workflow and CI - Plan is saved, reviewed, then applied.
  10. Debugging and anti-patterns - Common errors and what to avoid.

Examples use AWS for concreteness. The structure applies to any provider.

Examples

| Task | Jump to |

|------|---------|

| Layout for dev/staging/prod | Project Layout |

| Remote state with locking | Remote State |

| Validated variables | Variables and Outputs |

| Avoid secrets in state and code | Secrets |

| Rename a resource without destroying it | Refactoring Without Destroying |

| Plan/apply in CI | Safe Workflow and CI |

| Lock or drift errors | Debugging |


Project Layout

Keep environments as thin root modules. Put logic in reusable modules.

infra/
├── modules/
│   └── network/
│       ├── main.tf
│       ├── variables.tf
│       ├── outputs.tf
│       └── versions.tf
└── envs/
    ├── dev/
    │   ├── main.tf
    │   ├── backend.tf
    │   ├── providers.tf
    │   └── terraform.tfvars
    └── prod/
        ├── main.tf
        ├── backend.tf
        ├── providers.tf
        └── terraform.tfvars

Rules of thumb:

  • One state per environment and per blast-radius boundary (network, data, apps). A single giant state makes every plan slow and every mistake large.
  • Environments differ by inputs (terraform.tfvars), not by copy-pasted resources.
  • Do not use workspaces as a substitute for separate production and non-production state with separate credentials.

Versions and Providers

Pin everything. Unpinned providers change behavior between runs.

# versions.tf
terraform {
  required_version = ">= 1.10.0, < 2.0.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.40"
    }
  }
}
# providers.tf
provider "aws" {
  region = var.region

  default_tags {
    tags = {
      Environment = var.environment
      ManagedBy   = "terraform"
      Owner       = var.owner
    }
  }
}
  • Commit .terraform.lock.hcl. It records provider checksums so CI and teammates install identical providers.
  • Do not commit the .terraform/ directory or any *.tfstate file.
  • Upgrade deliberately: terraform init -upgrade, review the lock file diff, then plan.

Remote State

Local state does not work for teams and is easy to lose. Use a remote backend with encryption and locking.

# backend.tf
terraform {
  backend "s3" {
    bucket       = "example-org-tfstate-prod"
    key          = "network/terraform.tfstate"
    region       = "us-east-1"
    encrypt      = true
    use_lockfile = true # S3-native locking (Terraform >= 1.10)
  }
}

Older Terraform versions use a DynamoDB table for locking instead. Replace

use_lockfile = true with:

dynamodb_table = "example-org-tfstate-lock"

Backend rules:

  • Backend blocks cannot reference variables. Pass values with -backend-config or a partial config file when you need per-environment values.
  • Enable bucket versioning so a bad state write can be recovered.
  • Block public access on the state bucket and restrict who can read it. State often contains sensitive values.
  • Never use -lock=false to get past a lock error. See Debugging.

Variables and Outputs

Type and validate inputs so mistakes fail at plan time.

variable "environment" {
  type        = string
  description = "Deployment environment."

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

variable "instance_count" {
  type        = number
  description = "Number of application instances."
  default     = 2

  validation {
    condition     = var.instance_count >= 1 && var.instance_count <= 20
    error_message = "instance_count must be between 1 and 20."
  }
}

variable "allowed_cidrs" {
  type        = list(string)
  description = "CIDR blocks allowed to reach the service."
  default     = []
}
评论
Aaffaan-m
affaan-m
affaan-m/ECC
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@affaan-m
所属大类工具 Tools / 调试工具
国内可用性需海外环境
最近源活动2026-11-20 09:13
风险提示执行命令、访问网络
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。