63 lines
2.4 KiB
Markdown
63 lines
2.4 KiB
Markdown
---
|
||
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]]
|