terraform-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTerraform Best Practices
Terraform最佳实践
Comprehensive optimization guide for Terraform and Infrastructure as Code, maintained by Terramate. Contains 37 rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
这是由Terramate维护的Terraform与基础设施即代码(IaC)综合优化指南,涵盖10个类别共37条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Terraform modules or configurations
- Implementing infrastructure patterns (AWS, GCP, Azure, etc.)
- Reviewing code for security and reliability issues
- Refactoring existing Terraform/OpenTofu code
- Optimizing state management and performance
- Setting up team workflows and governance
在以下场景中可参考本指南:
- 编写新的Terraform模块或配置
- 实现基础设施架构(AWS、GCP、Azure等)
- 评审代码中的安全与可靠性问题
- 重构现有Terraform/OpenTofu代码
- 优化状态管理与性能
- 搭建团队工作流与治理规范
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Organization & Workflow | CRITICAL | |
| 2 | State Management | CRITICAL | |
| 3 | Security Best Practices | CRITICAL | |
| 4 | Module Design | HIGH | |
| 5 | Resource Organization | MEDIUM-HIGH | |
| 6 | Variable & Output Patterns | MEDIUM | |
| 7 | Language Best Practices | MEDIUM | |
| 8 | Provider Configuration | MEDIUM | |
| 9 | Performance Optimization | LOW-MEDIUM | |
| 10 | Testing & Validation | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 组织与工作流 | 关键 | |
| 2 | 状态管理 | 关键 | |
| 3 | 安全最佳实践 | 关键 | |
| 4 | 模块设计 | 高 | |
| 5 | 资源组织 | 中高 | |
| 6 | 变量与输出模式 | 中 | |
| 7 | 语言最佳实践 | 中 | |
| 8 | 提供商配置 | 中 | |
| 9 | 性能优化 | 中低 | |
| 10 | 测试与验证 | 低 | |
Quick Reference
快速参考
1. Organization & Workflow (CRITICAL) - 5 rules
1. 组织与工作流(关键)- 5条规则
- - All Terraform code in version control
org-version-control - - One workspace per environment per configuration
org-workspaces - - Control who can change what infrastructure
org-access-control - - Formal process for infrastructure changes
org-change-workflow - - Track all infrastructure changes
org-audit-logging
- - 所有Terraform代码纳入版本控制
org-version-control - - 每个配置的每个环境对应一个工作区
org-workspaces - - 控制基础设施变更权限
org-access-control - - 建立基础设施变更的正式流程
org-change-workflow - - 跟踪所有基础设施变更
org-audit-logging
2. State Management (CRITICAL) - 3 rules
2. 状态管理(关键)- 3条规则
- - Always use remote state backends
state-remote-backend - - Enable state locking to prevent corruption
state-locking - - Import existing infrastructure into Terraform
state-import
- - 始终使用远程状态后端
state-remote-backend - - 启用状态锁定以防止数据损坏
state-locking - - 将现有基础设施导入Terraform管理
state-import
3. Security Best Practices (CRITICAL) - 3 rules
3. 安全最佳实践(关键)- 3条规则
- - Never hardcode secrets in code
security-no-hardcoded-secrets - - Use proper credential management (OIDC, Vault, IAM roles)
security-credentials - - Follow least privilege principle
security-iam-least-privilege
- - 切勿在代码中硬编码密钥
security-no-hardcoded-secrets - - 使用合规的凭证管理方式(OIDC、Vault、IAM角色)
security-credentials - - 遵循最小权限原则
security-iam-least-privilege
4. Module Design (HIGH) - 5 rules
4. 模块设计(高)- 5条规则
- - One module per logical component
module-single-responsibility - - Use consistent naming conventions (terraform-<PROVIDER>-<NAME>)
module-naming - - Version all module references
module-versioning - - Compose modules like building blocks
module-composition - - Use existing community/shared modules
module-registry
- - 每个模块对应一个逻辑组件
module-single-responsibility - - 使用统一命名规范(terraform-<提供商>-<名称>)
module-naming - - 为所有模块引用添加版本控制
module-versioning - - 像搭建积木一样组合模块
module-composition - - 使用现有社区/共享模块
module-registry
5. Resource Organization (MEDIUM-HIGH) - 5 rules
5. 资源组织(中高)- 5条规则
- - Use consistent naming conventions
resource-naming - - Tag all resources for cost tracking
resource-tagging - - Use lifecycle blocks (prevent_destroy, ignore_changes)
resource-lifecycle - - Prefer for_each over count
resource-count-vs-foreach - - Prefer immutable infrastructure patterns
resource-immutable
- - 使用统一命名规范
resource-naming - - 为所有资源添加标签以追踪成本
resource-tagging - - 使用生命周期块(prevent_destroy、ignore_changes)
resource-lifecycle - - 优先使用for_each而非count
resource-count-vs-foreach - - 优先采用不可变基础设施模式
resource-immutable
6. Variable & Output Patterns (MEDIUM) - 6 rules
6. 变量与输出模式(中)- 6条规则
- - Use specific types, positive naming, nullable
variable-types - - Add validation rules for early error detection
variable-validation - - Mark secrets as sensitive, no defaults
variable-sensitive - - Document all variables with descriptions
variable-descriptions - - Document all outputs with descriptions
output-descriptions - - Never output secrets directly
output-no-secrets
- - 使用特定类型、正向命名、可空设置
variable-types - - 添加验证规则以提前发现错误
variable-validation - - 将密钥标记为敏感字段,不设默认值
variable-sensitive - - 为所有变量添加描述文档
variable-descriptions - - 为所有输出添加描述文档
output-descriptions - - 切勿直接输出密钥
output-no-secrets
7. Language Best Practices (MEDIUM) - 5 rules
7. 语言最佳实践(中)- 5条规则
- - Use jsonencode/yamlencode, not HEREDOC
language-no-heredoc-json - - Use locals to name complex expressions
language-locals - - Run terraform fmt and tflint
language-linting - - Use data sources instead of hardcoding
language-data-sources - - Use dynamic blocks for DRY code
language-dynamic-blocks
- - 使用jsonencode/yamlencode,而非HEREDOC
language-no-heredoc-json - - 使用locals命名复杂表达式
language-locals - - 运行terraform fmt和tflint
language-linting - - 使用数据源而非硬编码
language-data-sources - - 使用动态块实现DRY代码
language-dynamic-blocks
8. Provider Configuration (MEDIUM) - 1 rule
8. 提供商配置(中)- 1条规则
- - Pin provider versions
provider-version-constraints
- - 固定提供商版本
provider-version-constraints
9. Performance Optimization (LOW-MEDIUM) - 2 rules
9. 性能优化(中低)- 2条规则
- - Tune parallelism for large deployments
perf-parallelism - - Enable debug logging for troubleshooting
perf-debug
- - 为大规模部署调整并行度
perf-parallelism - - 启用调试日志以排查问题
perf-debug
10. Testing & Validation (LOW) - 2 rules
10. 测试与验证(低)- 2条规则
- - Testing pyramid (validate, lint, plan, integration)
test-strategies - - Implement policy checks (OPA, Checkov, tfsec)
test-policy-as-code
- - 测试金字塔(验证、 lint、计划、集成)
test-strategies - - 实施策略检查(OPA、Checkov、tfsec)
test-policy-as-code
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/state-remote-backend.md
rules/security-no-hardcoded-secrets.md
rules/module-versioning.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件获取详细说明与代码示例:
rules/state-remote-backend.md
rules/security-no-hardcoded-secrets.md
rules/module-versioning.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息与参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有扩展规则的完整指南,请查阅:
AGENTS.md