name: terraform-module-library description: 构建可重用的Terraform模块,用于AWS、Azure和GCP基础设施,遵循基础设施即代码最佳实践。适用于创建基础设施模块、标准化云资源供应或实施可重用的IaC组件时使用。
Terraform模块库
适用于AWS、Azure和GCP基础设施的生产级Terraform模块模式。
目的
创建可重用、经过良好测试的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 # 提供者版本
├── README.md # 文档
├── examples/ # 使用示例
│ └── complete/
│ ├── main.tf
│ └── variables.tf
└── tests/ # Terratest文件
└── module_test.go
AWS VPC模块示例
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
)
}
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块必须是有效的IPv4 CIDR表示法。"
}
}
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 = {}
}
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/目录中
- 使用验证块进行输入验证
- 输出重要属性用于模块组合
- 固定提供者版本在versions.tf中
- 使用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模块示例references/aws-modules.md- AWS模块模式references/azure-modules.md- Azure模块模式references/gcp-modules.md- GCP模块模式
测试
// tests/vpc_test.go
package test
import (
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)
func TestVPCModule(t *testing.T) {
terraformOptions := &terraform.Options{
TerraformDir: "../examples/complete",
}
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
vpcID := terraform.Output(t, terraformOptions, "vpc_id")
assert.NotEmpty(t, vpcID)
}
相关技能
multi-cloud-architecture- 用于架构决策cost-optimization- 用于成本效益设计