Skip to main content
skill CX Codex

refactor-module — Claude Code skill: split Terraform into modules

This skill turns a monolithic Terraform configuration into reusable modules with clear variables and outputs. Useful when one main.tf has grown too risky to touch.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add hashicorp/agent-skills --skill refactor-module -a claude-code
Open on GitHub

What refactor-module does

The skill reads your existing Terraform code and breaks it into modules following HashiCorp's design principles. It starts with analysis: resources get grouped by logical function, repeated patterns surface, dependencies get mapped, and the difficulty of moving state is assessed. Then comes interface design — input variables with validation, plus outputs.

After that the code is rewritten. Repeated resource blocks collapse into for_each and locals, and a root configuration calls the module through source. State migration is its own step: before writing moved blocks or state mv commands, the agent inspects current state with terraform state list and terraform show -json to match old resource addresses to new ones. That is where things break — if the addresses do not line up, terraform plan reports destroy-and-recreate instead of a move.

Inputs are the path to the configuration, the module name, and whether state compatibility must be preserved. Abstraction level is one of "simple", "intermediate" or "advanced", with intermediate as the default. You need access to state and a grasp of how your resources depend on each other; without that the migration cannot be planned.

What it helps with

  • split an oversized main.tf into modules with clear boundaries
  • fold repeated resource blocks into one module using for_each
  • define variables and outputs as an explicit module contract
  • plan a state move that avoids recreating live resources
  • separate resources sharing a lifecycle from cross-cutting concerns like tagging

Example prompts for the agent

  • refactor the config in ./infra into a vpc module and keep state intact
  • move subnets and the internet gateway into a network module, advanced abstraction
  • plan the state migration for the new module and show me the moved blocks

How to install refactor-module

1Skills CLI (Vercel)

npx skills add hashicorp/agent-skills --skill refactor-module -a claude-code

2Manually: put the files in place

~/.claude/skills/refactor-module/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/refactor-module/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about refactor-module

How do I install refactor-module in Claude?

Run npx skills add hashicorp/agent-skills --skill refactor-module -a claude-code. The skill lands in ~/.claude/skills/refactor-module/SKILL.md.

What does refactor-module do?

It breaks a monolithic Terraform configuration into modules: designing variables and outputs, rewriting resources, and planning the state migration.

What do I need before using it?

An existing Terraform configuration, access to state via terraform state list and terraform show -json, and an understanding of resource dependencies.

Similar skills

Everything for DevOps and infrastructure →

From the same set16

terraform-style-guide A skill that generates and validates Terraform code according to HashiCorp's official standards. Ensures consistent, secure, and readable HCL across your configurations. CX Codex +1 12,600installs terraform-test Guides an agent through Terraform test files, scenarios, assertions, and plan or apply runs. Use it when checking modules, resources, and outputs. CX Codex +1 8,800installs terraform-stacks Guides AI coding assistants through HashiCorp Terraform Stacks syntax, component definitions, and multi-environment deployment configs. CX Codex +1 5,600installs terraform-search-import Discovers untracked cloud assets and sets up Terraform configuration for bulk state imports. Helpful during infrastructure migrations and audits. CX Codex +1 4,900installs provider-resources This skill guides Terraform resource and data source work with the Plugin Framework. Use it when adding resources, handling drift, or reviewing provider code. CX Codex +1 4,100installs provider-actions Guides implementation of Terraform Actions with the Plugin Framework for operations run around resource create, update, and destroy events. CX Codex +1 3,900installs new-terraform-provider A skill that walks the agent through bootstrapping a Terraform provider on the Plugin Framework, from directory layout to a passing build. CX Codex +1 3,900installs run-acceptance-tests Runs and investigates Terraform provider acceptance tests with the TestAcc prefix. Use it for missing environment variables, failed runs, or tests that pass when they should not. CX Codex +1 3,700installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor