
关于
适用于 AWS、Azure 和 GCP 基础设施的生产级 Terraform 模块模式。
name: terraform-module-library description: "适用于 AWS、Azure 和 GCP 基础设施的生产就绪 Terraform 模块模式。" risk: unknown source: community date_added: "2026-02-27"
Terraform 模块库
适用于 AWS、Azure 和 GCP 基础设施的生产就绪 Terraform 模块模式。
不要在以下情况使用此技能
- 任务与 Terraform 模块库无关
- 你需要此范围之外的其他领域或工具
说明
- 明确目标、约束条件和所需输入。
- 应用相关最佳实践并验证结果。
- 提供可操作的步骤和验证方法。
- 如需详细示例,请打开
resources/implementation-playbook.md。
目的
为多个云提供商的常见云基础设施模式创建可复用、经过良好测试的 Terraform 模块。
在以下情况使用此技能
- 构建可复用的基础设施组件
- 标准化云资源配置
- 实施基础设施即代码最佳实践
- 创建多云兼容模块
- 建立组织级 Terraform 标准
模块结构
terraform-modules/
├── aws/
│ ├── vpc/
│ ├── eks/
│ ├── rds/
│ └── s3/
├── azure/
│ ├── vnet/
│ ├── aks/
│ └── storage/
└── gcp/
├── vpc/
├── gke/
└── cloud-sql/
标准模块模式
module-name/
├── main.tf # 主要资源
├── variables.tf # 输入变量
├── outputs.tf # 输出值
├── versions.tf # Provider 版本
├── README.md # 文档
├── examples/ # 使用示例
│ └── complete/
│ ├── main.tf
│ └── variables.tf
└── tests/ # Terratest 文件
└── module_test.go
AWS VPC 模块示例
main.tf:
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
tags = merge(
{
Name = var.name
},
var.tags
)
}
resource "aws_subnet" "private" {
count = length(var.private_subnet_cidrs)
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnet_cidrs[count.index]
availability_zone = var.availability_zones[count.index]
tags = merge(
{
Name = "${var.name}-private-${count.index + 1}"
Tier = "private"
},
var.tags
)
}
resource "aws_internet_gateway" "main" {
count = var.create_internet_gateway ? 1 : 0
vpc_id = aws_vpc.main.id
tags = merge(
{
Name = "${var.name}-igw"
},
var.tags
)
}
variables.tf:
variable "name" {
description = "VPC 名称"
type = string
}
variable "cidr_block" {
description = "VPC 的 CIDR 块"
type = string
validation {
condition = can(regex("^([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$", var.cidr_block))
error_message = "CIDR block must be valid IPv4 CIDR notation."
}
}
variable "availability_zones" {
description = "可用区列表"
type = list(string)
}
variable "private_subnet_cidrs" {
description = "私有子网的 CIDR 块"
type = list(string)
default = []
}
variable "enable_dns_hostnames" {
description = "在 VPC 中启用 DNS 主机名"
type = bool
default = true
}
variable "tags" {
description = "附加标签"
type = map(string)
default = {}
}
outputs.tf:
output "vpc_id" {
description = "VPC 的 ID"
value = aws_vpc.main.id
}
output "private_subnet_ids" {
description = "私有子网的 ID"
value = aws_subnet.private[*].id
}
output "vpc_cidr_block" {
description = "VPC 的 CIDR 块"
value = aws_vpc.main.cidr_block
}
最佳实践
- 使用语义化版本管理模块
- 为所有变量添加描述
- 在 examples/ 目录中提供示例
- 使用 validation 块进行输入验证
- 输出重要属性以便模块组合
- 在 versions.tf 中固定 provider 版本
- 使用 locals 处理计算值
- 使用 count/for_each 实现条件资源
- 使用 Terratest 测试模块
- 统一标记所有资源
模块组合
module "vpc" {
source = "../../modules/aws/vpc"
name = "production"
cidr_block = "10.0.0.0/16"
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
private_subnet_cidrs = [
"10.0.1.0/24",
"10.0.2.0/24",
"10.0.3.0/24"
]
tags = {
Environment = "production"
ManagedBy = "terraform"
}
}
module "rds" {
source = "../../modules/aws/rds"
identifier = "production-db"
engine = "postgres"
engine_version = "15.3"
instance_class = "db.t3.large"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnet_ids
tags = {
Environment = "production"
}
}
参考文件
assets/vpc-module/- 完整 VPC 模块示例assets/rds-module/- 完整 RDS 模块示例
兼容工具
Claude CodeCursor
标签
运维部署

