Files
nexus/wiki/concepts/Terraform-Modules.md
2026-04-24 00:03:01 +08:00

63 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Terraform Modules"
type: concept
sources: [ctp-topic-1-gruntwork-landing-zone-architecture, ctp-topic-3-deploy-and-maintain-infrastructure]
last_updated: 2026-04-14
---
## Definition
Terraform 模块Terraform Modules是一组可复用、经过测试的 Terraform 配置文件,用于构建具有业务上下文的 AWS 云服务。Gruntwork 的 Terraform AWS 服务目录Service Catalog提供了经过生产环境实战验证的模块集合强调服务应具有业务上下文而非简单的资源堆砌。
## Key Principles
### Business Context Over Raw Resources
- 传统 IaC定义单个 AWS 资源EC2 实例、安全组等)
- Gruntwork 模块:封装完整的服务逻辑(如"Web 应用服务"而非"VPC + EC2 + ALB"
- 模块包含服务所需的全部配置:网络、安全、监控、日志、备份等
### Reusability
- 一次编写,多处使用
- 版本化管理,通过 Terraform Module Registry 分发
- 支持参数化配置,适应不同环境
### Production-Ready
- 经过多个客户生产环境验证
- 内置安全最佳实践(加密、访问控制、审计日志)
- 包含监控和告警配置
- 支持灾难恢复和备份策略
## Module Structure
```
module/
├── main.tf # 主要资源定义
├── variables.tf # 输入变量
├── outputs.tf # 输出值
├── versions.tf # Terraform 版本约束
├── examples/ # 使用示例
└── test/ # TerraTest 测试
```
## Gruntwork Service Catalog
Gruntwork 提供的 Terraform 模块库,涵盖:
- 网络层VPC、子网、NAT Gateway、VPN
- 计算层ECS 集群、自动扩展组
- 数据层RDS 数据库、DynamoDB 表
- 安全层IAM 角色、KMS 密钥、安全组
- 监控层CloudWatch 告警、日志配置
## Relationship with Landing Zone
- Landing Zone 基于 Gruntwork 仓库创建
- 产品团队使用 Gruntwork 模块构建业务服务
- CI/CD 流水线自动化部署 Terraform 模块
## Related Concepts
- [[Landing-Zone-Architecture]]:使用 Terraform 模块构建 Landing Zone
- [[Terraform]]基础设施即代码工具Terraform 模块是其复用单元
- [[TerraTest]]:用于测试 Terraform 模块的 Go 测试框架
- [[CI-CD-Pipeline]]:自动化部署 Terraform 模块的流水线
## References
- [[ctp-topic-1-gruntwork-landing-zone-architecture]]
- [[ctp-topic-3-deploy-and-maintain-infrastructure]]
- [[ctp-topic-9-ci-cd-with-gruntwork]]