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.
npx skills add hashicorp/agent-skills --skill refactor-module -a claude-codeWhat 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.tfinto 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
./infrainto avpcmodule and keep state intact - move subnets and the internet gateway into a
networkmodule, advanced abstraction - plan the state migration for the new module and show me the
movedblocks
How to install refactor-module
1Skills CLI (Vercel)
npx skills add hashicorp/agent-skills --skill refactor-module -a claude-code2Manually: put the files in place
~/.claude/skills/refactor-module/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/refactor-module/
1Skills CLI (Vercel)
npx skills add hashicorp/agent-skills --skill refactor-module -a codex2Manually: put the files in place
.agents/skills/refactor-module/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add hashicorp/agent-skills --skill refactor-module -a cursor2Manually: put the files in place
.cursor/skills/refactor-module/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install hashicorp/agent-skills refactor-module2Manually: put the files in place
.github/skills/refactor-module/SKILL.md1Skills CLI (Vercel)
npx skills add hashicorp/agent-skills --skill refactor-module -a gemini-cli2Platform CLI
gemini skills install https://github.com/hashicorp/agent-skills --path plugins/terraform/skills/refactor-moduleManually: put the files in place
.devin/skills/refactor-module/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add hashicorp/agent-skills --skill refactor-module -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
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.