mcpField
T
terraform-module-library

terraform-module-library

github.com/midudev云与 DevOps7.0k57 次安装1 周前更新
代码仓库

关于 terraform-module-library

用于在 AWS、Azure、GCP 和 OCI 上构建常见基础设施的可复用 Terraform 模块技能。内容涵盖模块结构、组合、验证、版本管理和基于 Terratest 的测试。

主要功能

  1. 01多云模块模式
  2. 02标准模块文件结构
  3. 03输入验证与输出
  4. 04模块组合示例
  5. 05Terratest 测试指南

适用场景

  1. 01构建可复用的基础设施组件
  2. 02统一云资源配置流程
  3. 03创建多云 Terraform 模块
  4. 04制定组织级 Terraform 标准

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/midudev/autoskills --skill terraform-module-library

安装后在 Claude Code 中输入/terraform-module-library确认已加载。

附带文件references/aws-modules.mdreferences/oci-modules.md

详细介绍

Terraform 模块库

适用于 AWS、Azure、GCP 和 OCI 基础设施的生产级 Terraform 模块模式。

目的

为跨多个云提供商的常见云基础设施模式创建可复用、经过充分测试的 Terraform 模块。

何时使用

  • 构建可复用的基础设施组件
  • 标准化云资源预配
  • 实施基础设施即代码最佳实践
  • 创建多云兼容的模块
  • 建立组织级 Terraform 标准

模块结构

terraform-modules/
├── aws/
│   ├── vpc/
│   ├── eks/
│   ├── rds/
│   └── s3/
├── azure/
│   ├── vnet/
│   ├── aks/
│   └── storage/
├── gcp/
│   ├── vpc/
│   ├── gke/
│   └── cloud-sql/
└── oci/
    ├── vcn/
    ├── oke/
    └── object-storage/

标准模块模式

module-name/
├── main.tf          # Main resources
├── variables.tf     # Input variables
├── outputs.tf       # Output values
├── versions.tf      # Provider versions
├── README.md        # Documentation
├── examples/        # Usage examples
│   └── complete/
│       ├── main.tf
│       └── variables.tf
└── tests/           # Terratest files
    └── 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 = "Name of the VPC"
  type        = string
}

variable "cidr_block" {
  description = "CIDR block for VPC"
  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 = "List of availability zones"
  type        = list(string)
}

variable "private_subnet_cidrs" {
  description = "CIDR blocks for private subnets"
  type        = list(string)
  default     = []
}

variable "enable_dns_hostnames" {
  description = "Enable DNS hostnames in VPC"
  type        = bool
  default     = true
}

variable "tags" {
  description = "Additional tags"
  type        = map(string)
  default     = {}
}

outputs.tf:

output "vpc_id" {
  description = "ID of the VPC"
  value       = aws_vpc.main.id
}

output "private_subnet_ids" {
  description = "IDs of private subnets"
  value       = aws_subnet.private[*].id
}

output "vpc_cidr_block" {
  description = "CIDR block of VPC"
  value       = aws_vpc.main.cidr_block
}

最佳实践

  1. 为模块使用语义化版本控制
  2. 记录所有变量,并附上说明
  3. 在 examples/ 目录中提供示例
  4. 使用 validation 块进行输入验证
  5. 输出重要属性以便模块组合
  6. 在 versions.tf 中固定提供商版本
  7. 对计算值使用 locals
  8. 使用 count/for_each 实现条件资源
  9. 使用 Terratest 测试模块
  10. 一致地为所有资源添加标签

参考: 参见 references/aws-modules.md 和 references/oci-modules.md

模块组合

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"
  }
}

测试

// 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 - 用于成本效益设计

评论

支持 Markdown