mcpField
T
terraform-module-library

terraform-module-library

github.com/hermeticormusクラウド・DevOps12375 回インストール4 週間前に更新
リポジトリ

terraform-module-library について

AWS、Azure、GCP 向けの再利用可能な Terraform インフラモジュールを作成するチーム向けのスキルです。モジュールのパターン、例、テスト方法、各クラウドの資料を提供します。

主な機能

  1. 01AWS、Azure、GCP 向けのパターン
  2. 02標準的なモジュール構成
  3. 03入力検証とリソース出力
  4. 04モジュールの合成と使用例
  5. 05Terratest によるテストの指針

活用シーン

  1. 01再利用可能なインフラコンポーネントの作成
  2. 02クラウドリソースのプロビジョニング標準化
  3. 03複数クラウド向けモジュールの構築
  4. 04組織内の Terraform 実践方法の策定

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/hermeticormus/libreuiux-claude-code --skill terraform-module-library

インストール後、Claude Code で/terraform-module-libraryを入力して読み込まれたか確認します。

同梱ファイルreferences/aws-modules.md

詳細

Terraform モジュールライブラリ

AWS、Azure、GCP のインフラストラクチャ向けの、本番環境で使用できる Terraform モジュールのパターンです。

目的

複数のクラウドプロバイダーにわたる一般的なクラウドインフラストラクチャのパターンに対応した、再利用可能で十分にテストされた Terraform モジュールを作成します。

使用する場面

  • 再利用可能なインフラストラクチャコンポーネントを構築する
  • クラウドリソースのプロビジョニングを標準化する
  • ベストプラクティスに沿った Infrastructure as Code を実装する
  • マルチクラウドに対応するモジュールを作成する
  • 組織内の Terraform 標準を確立する

モジュール構成

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

標準的なモジュールパターン

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. すべてのリソースに一貫性のあるタグを付ける

モジュールの組み合わせ

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 - コスト効率の高い設計に使用します

コメント