References: Terraform official documentation, Pulumi documentation, AWS CloudFormation documentation, HashiCorp Best Practices, Infrastructure as Code (Kief Morris).
Applicable Scenarios
- Cloud infrastructure initialization (VPC / EKS / RDS / S3)
- Multi-environment consistency management (dev / staging / prod)
- Infrastructure modularization and reuse
- Drift detection and remediation
- Infrastructure change auditing
- Rapid rebuild of disaster recovery environments
- Cost optimization (resource tags + lifecycle)
Core Principles
1. 声明式优先
描述期望状态,不描述步骤
工具负责收敛到目标状态
2. 状态集中管理
远程 backend(S3 / GCS / Terraform Cloud)
状态锁防止并发修改
3. 模块化复用
通用组件抽成模块
模块有版本 + 文档
4. 环境隔离
每环境独立状态文件
变量文件区分环境差异
5. 变更可审计
PR 触发 plan → 人工审批 → apply
所有变更有记录
6. 最小权限
IaC 执行角色只有必要权限
不使用 admin 角色
7. 不可变基础设施
修改 = 替换,不原地修改
减少配置漂移
Workflow
1. 规划基础设施
- 画架构图(VPC / 子网 / 安全组)
- 确定资源依赖关系
- 选择 IaC 工具
2. 初始化项目结构
- 目录按环境 / 层分离
- 配置远程 backend
- 设置状态锁
3. 编写模块
- 网络层(VPC / Subnet / NAT)
- 计算层(EKS / ECS / EC2)
- 数据层(RDS / ElastiCache / S3)
- 安全层(IAM / SG / KMS)
4. 环境变量化
- terraform.tfvars 按环境
- 敏感值从 Vault / SSM 读取
- 输出值供下游使用
5. CI 集成
- PR → terraform plan(自动评论)
- merge → terraform apply(审批后)
- 定时 → drift 检测
6. Drift 检测
- 定期 terraform plan 对比
- 告警非 IaC 变更
- 修复或导入
7. 文档与标签
- 资源标签(team / env / cost-center)
- 模块 README
- 变更日志
Project Structure (Terraform)
infrastructure/
├── modules/ ← 可复用模块
│ ├── networking/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ │ └── README.md
│ ├── eks-cluster/
│ └── rds/
├── environments/ ← 环境配置
│ ├── dev/
│ │ ├── main.tf ← 调用模块
│ │ ├── terraform.tfvars
│ │ └── backend.tf
│ ├── staging/
│ └── production/
├── .terraform-version
└── Makefile
Tool Comparison
| Feature | Terraform | Pulumi | CloudFormation |
|---|---|---|---|
| Language | HCL | TypeScript/Python/Go | YAML/JSON |
| State Management | Remote backend | Pulumi Cloud | AWS managed |
| Multi-cloud | ✅ | ✅ | ❌(AWS only) |
| Drift detection | plan | preview | drift detection |
| Module ecosystem | Rich Registry | Fewer | Nested stacks |
| Learning curve | Medium | Low (familiar language) | Medium |
Quality Self-Check
□ 远程 backend 已配置(S3 + DynamoDB 锁)
□ 状态文件不在 git 中
□ 模块有 variables / outputs / README
□ 敏感值不硬编码(用 SSM / Vault)
□ 资源有标签(Name / Environment / Team / CostCenter)
□ PR 自动运行 plan
□ plan 输出作为 PR 评论
□ apply 需审批
□ Drift 检测定期运行
□ 模块有版本锁定
□ 最小权限 IAM 角色
□ 有 destroy 保护(prevent_destroy)
□ 输出值供下游消费
Common Pitfalls
- Local storage of state files——team collaboration conflicts, loss is a disaster
- Not pinning provider versions——plan changes after upgrades are uncontrollable
- Hardcoding secrets——leaked to git
- Not using modules——copy-paste between environments, drift inconsistency
- Manually modifying resources——state drift, plan confusion
- Applying without approval——accidentally deleting production resources
- Large state files——slow plan, large blast radius
- Circular dependencies——modules referencing each other
- Not tagging——costs cannot be attributed
- Ignoring plan output——applying without seeing destroy
- Not doing import——existing resources recreated
- Workspace abuse——less clear than directory isolation
Supporting Templates
templates/terraform-module-template.md— Terraform module standard structure template
Collaboration with Other Skills
上游:
架构设计 → 基础设施需求
secrets-config → 敏感配置来源
下游:
kubernetes-orchestration → 集群基础设施
networking-gateway → 网络 / LB / DNS
ci-cd-pipeline → 基础设施部署阶段
monitoring-alerting → 监控基础设施