
Refactor Module
Turn a flat Terraform root module into reusable HashiCorp-style modules with safe state migration for a solo infra owner.
Install
npx skills add https://github.com/hashicorp/agent-skills --skill refactor-moduleWhat is this skill?
- Analyzes monolithic Terraform and splits along HashiCorp module design principles
- Defines variable and output contracts with encapsulation and abstraction levels
- Plans state compatibility and migration when preserve_state is required
- Covers versioning, documentation, and module testing frameworks
- Supports simple, intermediate, and advanced abstraction_level options
Adoption & trust: 2.6k installs on skills.sh; 654 GitHub stars; 3/3 security scanners passed (skills.sh audits).
Recommended Skills
Journey fit
Build/integrations is the shelf where Terraform modules wire cloud APIs and providers—refactoring here precedes routine Ship deploy and Operate infra work. Integrations captures provider resources and module boundaries rather than app frontend code, matching Terraform’s role in a solo builder stack.
Common Questions / FAQ
Is Refactor Module safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Refactor Module
# Skill: Refactor Module ## Overview This skill guides AI agents in transforming monolithic Terraform configurations into reusable, maintainable modules following HashiCorp's module design principles and community best practices. ## Capability Statement The agent will analyze existing Terraform code and systematically refactor it into well-structured modules with: - Clear interface contracts (variables and outputs) - Proper encapsulation and abstraction - Versioning and documentation - Testing frameworks - Migration path for existing state ## Prerequisites - Existing Terraform configuration to refactor - Understanding of resource dependencies - Access to current state file (for migration planning) - Knowledge of module registry patterns ## Input Parameters | Parameter | Type | Required | Description | |-----------|------|----------|-------------| | `source_directory` | string | Yes | Path to existing Terraform configuration | | `module_name` | string | Yes | Name for the new module | | `abstraction_level` | string | No | "simple", "intermediate", "advanced" (default: intermediate) | | `preserve_state` | boolean | Yes | Whether to maintain state compatibility | | `target_registry` | string | No | Target module registry (local, private, public) | ## Execution Steps ### 1. Analysis Phase ```markdown **Identify Refactoring Candidates** - Group resources by logical function - Identify repeated patterns - Map resource dependencies - Detect configuration coupling - Analyze variable usage patterns **Complexity Assessment** - Count resource relationships - Measure variable propagation depth - Identify cross-resource references - Evaluate state migration complexity ``` ### 2. Module Design #### Interface Design ```hcl # Define clear input contract variable "network_config" { description = "Network configuration parameters" type = object({ cidr_block = string availability_zones = list(string) enable_nat = bool }) validation { condition = can(cidrhost(var.network_config.cidr_block, 0)) error_message = "CIDR block must be valid IPv4 CIDR." } } # Define output contract output "vpc_id" { description = "ID of the created VPC" value = aws_vpc.main.id } output "private_subnet_ids" { description = "List of private subnet IDs" value = { for k, v in aws_subnet.private : k => v.id } } ``` #### Encapsulation Strategy ```markdown **What to Include in Module:** - Tightly coupled resources (VPC + subnets) - Resources with shared lifecycle - Configuration with clear boundaries **What to Keep Separate:** - Cross-cutting concerns (monitoring, tagging) - Resources with different lifecycles - Provider-specific configurations ``` ### 3. Code Transformation #### Before: Monolithic Configuration ```hcl # main.tf (monolithic) resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags = { Name = "production-vpc" Environment = "prod" } } resource "aws_subnet" "public_1" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" availability_zone = "us-east-1a" tags = { Name = "public-subnet-1" Type = "public" } } resource "aws_subnet" "public_2" { vpc_id = aws_vpc.main.id cidr_block = "10.0.2.0/24" availability_zone = "us-east-1b" tags = { Name = "public-subnet-2" Type = "public" } } resource "aws_internet_gateway" "main" { vpc_id = aws_vpc.main.id tags = { Name = "production-igw" } } # ... more repetitive subnet and routing resources ``` #### After: Modular Structure ```hcl # modules/vpc/main.tf locals { subnet_count = length(var.availability_zones) } resource "aws_vpc"