Terraform Module Library

作者 wshobson46891e7e60da無授權條款收錄於 2026年10月8日更新於 2026年10月8日

Build reusable Terraform modules for AWS, Azure, GCP, and OCI infrastructure following infrastructure-as-code best practices. Use when creating infrastructure modules, standardizing cloud provisioning, or implementing reusable IaC components.

僅含說明DevOps & Cloud
AI 產生的概覽

指導建立可重用的 Terraform 模組,涵蓋 AWS、Azure、GCP 與 OCI 基礎架構。

功能
此技能提供建立可重用 Terraform 模組的模式與慣例,涵蓋 AWS、Azure、GCP 和 OCI 上常見的雲端基礎架構,例如 VPC、Kubernetes 叢集、資料庫與儲存。它說明標準模組結構(main.tf、variables.tf、outputs.tf、versions.tf、examples 與 tests),提供含變數、輸出與驗證的 AWS VPC 完整範例,並列出語意化版本、固定 provider 版本、一致加上標籤及 Terratest 測試等最佳實務。它也說明模組組合,也就是把一個模組的輸出接到另一個模組的輸入。兩份參考文件分別說明 AWS 與 OCI 模組細節。
適用情境
適用於建立可重用基礎架構模組、統一跨雲廠商的資源佈建,或制定組織層級的 Terraform 標準。也適合想落實基礎架構即程式碼最佳實務、打造多雲相容元件的團隊。
執行需求
需要 Terraform 及其 HCL 設定語言,以及所針對的 AWS、Azure、GCP 或 OCI 雲端憑證。測試範例依賴 Go、Terratest 與 testify。此技能未附任何指令碼,僅有說明文件與參考文件。

Terraform Module Library

Production-ready Terraform module patterns for AWS, Azure, GCP, and OCI infrastructure.

Purpose

Create reusable, well-tested Terraform modules for common cloud infrastructure patterns across multiple cloud providers.

When to Use

  • Build reusable infrastructure components
  • Standardize cloud resource provisioning
  • Implement infrastructure as code best practices
  • Create multi-cloud compatible modules
  • Establish organizational Terraform standards

Module Structure

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

Standard Module Pattern

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 Module Example

main.tf:

hcl
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:

hcl
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:

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

Best Practices

  1. Use semantic versioning for modules
  2. Document all variables with descriptions
  3. Provide examples in examples/ directory
  4. Use validation blocks for input validation
  5. Output important attributes for module composition
  6. Pin provider versions in versions.tf
  7. Use locals for computed values
  8. Implement conditional resources with count/for_each
  9. Test modules with Terratest
  10. Tag all resources consistently

Reference: See references/aws-modules.md and references/oci-modules.md

Module Composition

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

Testing

go
// tests/vpc_test.gopackage 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)}

Related Skills

  • multi-cloud-architecture - For architectural decisions
  • cost-optimization - For cost-effective designs

來源與署名

來源:wshobson/agents位於plugins/cloud-infrastructure/skills/terraform-module-library提交46891e7

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架