Build reusable Terraform modules for AWS, Azure, and GCP infrastructure following infrastructure-as-code best practices. Use when creating infrastructure modules, standardizing cloud provisioning, or implementing reusable IaC components.
Add this skill
npx mdskills install sickn33/terraform-module-libraryComprehensive infrastructure-as-code patterns with excellent examples and validation practices
1---2name: terraform-module-library3description: Build reusable Terraform modules for AWS, Azure, and GCP infrastructure following infrastructure-as-code best practices. Use when creating infrastructure modules, standardizing cloud provisioning, or implementing reusable IaC components.4---56# Terraform Module Library78Production-ready Terraform module patterns for AWS, Azure, and GCP infrastructure.910## Do not use this skill when1112- The task is unrelated to terraform module library13- You need a different domain or tool outside this scope1415## Instructions1617- Clarify goals, constraints, and required inputs.18- Apply relevant best practices and validate outcomes.19- Provide actionable steps and verification.20- If detailed examples are required, open `resources/implementation-playbook.md`.2122## Purpose2324Create reusable, well-tested Terraform modules for common cloud infrastructure patterns across multiple cloud providers.2526## Use this skill when2728- Build reusable infrastructure components29- Standardize cloud resource provisioning30- Implement infrastructure as code best practices31- Create multi-cloud compatible modules32- Establish organizational Terraform standards3334## Module Structure3536```37terraform-modules/38├── aws/39│ ├── vpc/40│ ├── eks/41│ ├── rds/42│ └── s3/43├── azure/44│ ├── vnet/45│ ├── aks/46│ └── storage/47└── gcp/48 ├── vpc/49 ├── gke/50 └── cloud-sql/51```5253## Standard Module Pattern5455```56module-name/57├── main.tf # Main resources58├── variables.tf # Input variables59├── outputs.tf # Output values60├── versions.tf # Provider versions61├── README.md # Documentation62├── examples/ # Usage examples63│ └── complete/64│ ├── main.tf65│ └── variables.tf66└── tests/ # Terratest files67 └── module_test.go68```6970## AWS VPC Module Example7172**main.tf:**73```hcl74resource "aws_vpc" "main" {75 cidr_block = var.cidr_block76 enable_dns_hostnames = var.enable_dns_hostnames77 enable_dns_support = var.enable_dns_support7879 tags = merge(80 {81 Name = var.name82 },83 var.tags84 )85}8687resource "aws_subnet" "private" {88 count = length(var.private_subnet_cidrs)89 vpc_id = aws_vpc.main.id90 cidr_block = var.private_subnet_cidrs[count.index]91 availability_zone = var.availability_zones[count.index]9293 tags = merge(94 {95 Name = "${var.name}-private-${count.index + 1}"96 Tier = "private"97 },98 var.tags99 )100}101102resource "aws_internet_gateway" "main" {103 count = var.create_internet_gateway ? 1 : 0104 vpc_id = aws_vpc.main.id105106 tags = merge(107 {108 Name = "${var.name}-igw"109 },110 var.tags111 )112}113```114115**variables.tf:**116```hcl117variable "name" {118 description = "Name of the VPC"119 type = string120}121122variable "cidr_block" {123 description = "CIDR block for VPC"124 type = string125 validation {126 condition = can(regex("^([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$", var.cidr_block))127 error_message = "CIDR block must be valid IPv4 CIDR notation."128 }129}130131variable "availability_zones" {132 description = "List of availability zones"133 type = list(string)134}135136variable "private_subnet_cidrs" {137 description = "CIDR blocks for private subnets"138 type = list(string)139 default = []140}141142variable "enable_dns_hostnames" {143 description = "Enable DNS hostnames in VPC"144 type = bool145 default = true146}147148variable "tags" {149 description = "Additional tags"150 type = map(string)151 default = {}152}153```154155**outputs.tf:**156```hcl157output "vpc_id" {158 description = "ID of the VPC"159 value = aws_vpc.main.id160}161162output "private_subnet_ids" {163 description = "IDs of private subnets"164 value = aws_subnet.private[*].id165}166167output "vpc_cidr_block" {168 description = "CIDR block of VPC"169 value = aws_vpc.main.cidr_block170}171```172173## Best Practices1741751. **Use semantic versioning** for modules1762. **Document all variables** with descriptions1773. **Provide examples** in examples/ directory1784. **Use validation blocks** for input validation1795. **Output important attributes** for module composition1806. **Pin provider versions** in versions.tf1817. **Use locals** for computed values1828. **Implement conditional resources** with count/for_each1839. **Test modules** with Terratest18410. **Tag all resources** consistently185186## Module Composition187188```hcl189module "vpc" {190 source = "../../modules/aws/vpc"191192 name = "production"193 cidr_block = "10.0.0.0/16"194 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]195196 private_subnet_cidrs = [197 "10.0.1.0/24",198 "10.0.2.0/24",199 "10.0.3.0/24"200 ]201202 tags = {203 Environment = "production"204 ManagedBy = "terraform"205 }206}207208module "rds" {209 source = "../../modules/aws/rds"210211 identifier = "production-db"212 engine = "postgres"213 engine_version = "15.3"214 instance_class = "db.t3.large"215216 vpc_id = module.vpc.vpc_id217 subnet_ids = module.vpc.private_subnet_ids218219 tags = {220 Environment = "production"221 }222}223```224225## Reference Files226227- `assets/vpc-module/` - Complete VPC module example228- `assets/rds-module/` - RDS module example229- `references/aws-modules.md` - AWS module patterns230- `references/azure-modules.md` - Azure module patterns231- `references/gcp-modules.md` - GCP module patterns232233## Testing234235```go236// tests/vpc_test.go237package test238239import (240 "testing"241 "github.com/gruntwork-io/terratest/modules/terraform"242 "github.com/stretchr/testify/assert"243)244245func TestVPCModule(t *testing.T) {246 terraformOptions := &terraform.Options{247 TerraformDir: "../examples/complete",248 }249250 defer terraform.Destroy(t, terraformOptions)251 terraform.InitAndApply(t, terraformOptions)252253 vpcID := terraform.Output(t, terraformOptions, "vpc_id")254 assert.NotEmpty(t, vpcID)255}256```257258## Related Skills259260- `multi-cloud-architecture` - For architectural decisions261- `cost-optimization` - For cost-effective designs262
Full transparency — inspect the skill content before installing.