terraform-modules
$
npx mdskill add TheBushidoCollective/han/terraform-modulesBuild reusable infrastructure code with Terraform modules.
- Organize complex cloud resources into standardized components.
- Integrates with Terraform CLI and HCL configuration syntax.
- Generates modular code based on defined variables and outputs.
- Delivers structured module files ready for deployment.
SKILL.md
.github/skills/terraform-modulesView on GitHub ↗
---
name: terraform-modules
user-invocable: false
description: Use when creating and using reusable Terraform modules for organizing and sharing infrastructure code.
allowed-tools: []
---
# Terraform Modules
Creating and using reusable Terraform modules.
## Module Structure
```
modules/vpc/
├── main.tf
├── variables.tf
├── outputs.tf
└── README.md
```
## Creating a Module
### main.tf
```hcl
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
enable_dns_hostnames = var.enable_dns_hostnames
tags = merge(var.tags, {
Name = var.name
})
}
resource "aws_subnet" "public" {
count = length(var.public_subnets)
vpc_id = aws_vpc.main.id
cidr_block = var.public_subnets[count.index]
availability_zone = var.availability_zones[count.index]
tags = merge(var.tags, {
Name = "${var.name}-public-${count.index + 1}"
})
}
```
### variables.tf
```hcl
variable "name" {
description = "VPC name"
type = string
}
variable "cidr_block" {
description = "VPC CIDR block"
type = string
}
variable "public_subnets" {
description = "Public subnet CIDR blocks"
type = list(string)
default = []
}
variable "tags" {
description = "Resource tags"
type = map(string)
default = {}
}
```
### outputs.tf
```hcl
output "vpc_id" {
description = "VPC ID"
value = aws_vpc.main.id
}
output "public_subnet_ids" {
description = "Public subnet IDs"
value = aws_subnet.public[*].id
}
```
## Using Modules
### Local Module
```hcl
module "vpc" {
source = "./modules/vpc"
name = "production-vpc"
cidr_block = "10.0.0.0/16"
public_subnets = [
"10.0.1.0/24",
"10.0.2.0/24",
]
tags = {
Environment = "production"
}
}
# Access module outputs
resource "aws_instance" "web" {
subnet_id = module.vpc.public_subnet_ids[0]
}
```
### Registry Module
```hcl
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = false
}
```
### Git Module
```hcl
module "vpc" {
source = "git::https://github.com/org/terraform-modules.git//vpc?ref=v1.0.0"
name = "my-vpc"
# ...
}
```
## Module Composition
```hcl
module "network" {
source = "./modules/network"
name = var.name
}
module "compute" {
source = "./modules/compute"
vpc_id = module.network.vpc_id
subnet_id = module.network.subnet_ids[0]
}
module "database" {
source = "./modules/database"
vpc_id = module.network.vpc_id
subnet_ids = module.network.private_subnet_ids
}
```
## For_each with Modules
```hcl
variable "applications" {
type = map(object({
instance_type = string
ami_id = string
}))
}
module "application" {
for_each = var.applications
source = "./modules/application"
name = each.key
instance_type = each.value.instance_type
ami_id = each.value.ami_id
}
```
## Count with Modules
```hcl
module "worker" {
count = var.worker_count
source = "./modules/worker"
name = "worker-${count.index + 1}"
index = count.index
}
```
## Module Best Practices
### Version Pinning
```hcl
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0" # Allow patch updates
}
```
### Input Validation
```hcl
variable "environment" {
type = string
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Environment must be dev, staging, or prod."
}
}
```
### Output Everything Useful
```hcl
output "vpc_id" {
value = aws_vpc.main.id
}
output "vpc_cidr" {
value = aws_vpc.main.cidr_block
}
output "subnet_ids" {
value = aws_subnet.main[*].id
}
```
### Use Consistent Naming
```hcl
variable "name_prefix" {
type = string
}
locals {
name = "${var.name_prefix}-${var.environment}"
}
```
## Publishing Modules
### Module Registry Format
```
terraform-<PROVIDER>-<NAME>
terraform-aws-vpc
terraform-google-network
```
### Semantic Versioning
```
v1.0.0 - Major release
v1.1.0 - Minor release
v1.1.1 - Patch release
```
More from TheBushidoCollective/han
- absinthe-resolversUse when implementing GraphQL resolvers with Absinthe. Covers resolver patterns, dataloader integration, batching, and error handling.
- absinthe-schemaUse when designing GraphQL schemas with Absinthe. Covers type definitions, interfaces, unions, enums, and schema organization patterns.
- absinthe-subscriptionsUse when implementing real-time GraphQL subscriptions with Absinthe. Covers Phoenix channels, PubSub, and subscription patterns.
- act-docker-setupUse when configuring Docker environments for act, selecting runner images, managing container resources, or troubleshooting Docker-related issues with local GitHub Actions testing.
- act-local-testingUse when testing GitHub Actions workflows locally with act. Covers act CLI usage, Docker configuration, debugging workflows, and troubleshooting common issues when running workflows on your local machine.
- act-workflow-syntaxUse when creating or modifying GitHub Actions workflow files. Provides guidance on workflow syntax, triggers, jobs, steps, and expressions for creating valid GitHub Actions workflows that can be tested locally with act.
- ameba-configurationUse when configuring Ameba rules and settings for Crystal projects including .ameba.yml setup, rule management, severity levels, and code quality enforcement.
- ameba-custom-rulesUse when creating custom Ameba rules for Crystal code analysis including rule development, AST traversal, issue reporting, and rule testing.
- ameba-integrationUse when integrating Ameba into development workflows including CI/CD pipelines, pre-commit hooks, GitHub Actions, and automated code review processes.
- analyze-performanceAnalyze performance metrics and identify slow transactions in Sentry