terraform-refactor-module

Refactor monolithic Terraform configurations into reusable modules with state migration.

Updated Jun 13, 2026
One-click install
npx skills add https://github.com/jenreh/project-kit-template --skill terraform-refactor-module-jenreh
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: terraform-refactor-module
Source: https://github.com/jenreh/project-kit-template/tree/main/.agents/agent-skills/skills/terraform-refactor-module
Command: npx skills add https://github.com/jenreh/project-kit-template --skill terraform-refactor-module-jenreh

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Monolithic Terraform configurations become hard to maintain, reuse, and test as infrastructure grows. This Skill guides the systematic transformation of flat Terraform code into well-structured, reusable modules without breaking existing state. ## Core Features & Use Cases - Module Extraction: Analyze existing Terraform code, group resources by logical function, and convert them into modules with typed variables, validation rules, and outputs. - State Migration: Generate moved blocks (Terraform 1.1+) or terraform state mv commands so refactoring causes no resource recreation. - Documentation & Testing: Produce module README documentation with inputs/outputs tables and structure .tftest.hcl test files for unit and integration testing. - Use Case: You have a 500-line main.tf defining a VPC, subnets, and gateways. Use this Skill to extract a versioned modules/vpc module, migrate state safely, and verify terraform plan shows no changes. ## Quick Start Refactor the Terraform configuration in ./infra into a reusable VPC module while preserving the existing state.

Frequently Asked Questions about terraform-refactor-module

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor Terraform code into modules?

Group resources by logical function, extract them into a module directory with variables.tf and outputs.tf, then reference the module from the root configuration. Use moved blocks or terraform state mv to migrate existing state so no resources are recreated.

How to migrate Terraform state when moving resources into a module?

On Terraform 1.1+, add moved blocks mapping old resource addresses to new module addresses, then run terraform plan to confirm no changes. On older versions, use terraform state mv commands for each resource.

What makes a good Terraform module interface?

A good module interface uses typed variables with descriptions and validation blocks, plus outputs that expose the IDs and attributes consumers need. Avoid overly generic map(any) inputs that cannot be validated.

Does Terraform refactoring destroy existing resources?

Not if state is migrated correctly. After adding moved blocks or running state mv commands, terraform plan should show no changes; apply only when the plan is empty to avoid resource recreation.

How do I test a Terraform module?

Create .tftest.hcl files in a tests/ directory with run blocks and assertions. Use plan mode for unit tests and apply mode for integration tests; mock providers are available since Terraform 1.7.