All skills
acedergren avatar

/refactor-module

@9d099e9

Use when deciding whether to extract Terraform code into a reusable module, determining module boundaries, or migrating state after modularization. Covers the refactoring decision (modularize vs inline), anti-patterns causing module sprawl, and state migration risk. Keywords: terraform module, refactor terraform, module boundaries, terraform abstraction, module sprawl, state migration, terraform state mv. Triggers on "refactor Terraform module" or "decide module boundary".

Use this Skill: https://skilld.dev/gh/acedergren/agentic-tools/refactor-module

This session only. Nothing lands on disk.

SKILL.md

β‰ˆ124 tokens always: the name and description. β‰ˆ1.3k when used: this file. β‰ˆ1.7k more on demand in 2 files.

Terraform Module Refactoring - Decision Expert

When to Use

Load this skill when the user request matches the frontmatter description for Terraform Module Refactoring - Decision Expert.

Assumption: You know Terraform syntax. This covers when to modularize vs keep inline.

NEVER

  • Never modularize on first usage β€” wait for the third real instance; premature abstraction locks in the wrong seam.
  • Never expose module variables 1:1 with resource arguments β€” that's not abstraction, it's indirection (the Leaky Abstraction trap).
  • Never refactor inline resources to a module without running terraform state mv first β€” Terraform will plan to destroy and recreate every resource.
  • Never create a module for frequently-changing code β€” module API changes cascade across all consumers.
  • Never skip terraform state pull > backup.tfstate before any state migration.

The Core Decision

Considering creating a module?
β”‚
β”œβ”€ Used once β†’ NEVER modularize (keep inline, wait for third)
β”‚   WHY: Premature abstraction = wrong seam baked in early
β”‚
β”œβ”€ Used 2–3 times β†’ MAYBE
β”‚   β”œβ”€ >80% identical config β†’ modularize
β”‚   β”œβ”€ <50% identical β†’ use locals instead
β”‚   └─ Different teams β†’ DON'T (coordination overhead > benefit)
β”‚
β”œβ”€ Used 4+ times β†’ Modularize IF config stable (not changing every sprint)
β”‚   WHY: Module changes = N consumer PRs; unstable API kills teams
β”‚
└─ Compliance/security requirement β†’ Modularize immediately
    WHY: Module = single enforcement point across all consumers

Break-even: Module worth it at 4+ identical usages + stable API + compliance need. Time cost: Simple module = 2 hours. Complex with state migration = 2 days planning + 4 hours execution.

Before Extracting: Strategic Check

Question Threshold Decision
How many usages? <3 Keep inline
How identical? <50% same Use locals, not module
Change frequency Weekly DON'T (unstable API)
Test coverage <50% TOO RISKY (breaking changes uncaught)
Consumer count 10+ Every change = 10 PRs, plan migration carefully

Anti-Patterns

Leaky Abstraction (most common)

Signal: Module variables match resource arguments 1:1 (50 variables for a VPC module).

Fix: Expose intent, not resource config:

// Instead of 50 variables:
variable "network_config" {
  type = object({ cidr = string, azs = list(string), public_subnets = number, private_subnets = number })
}

Test: "Does the module consumer need AWS VPC knowledge to use this?" YES = leaky abstraction.

State Migration Trap (most dangerous)

Moving inline resources into a module changes state addresses: aws_vpc.main β†’ module.network.aws_vpc.main

Terraform reads this as "destroy old, create new" β€” no warning, identical config, production outage.

# Always: backup β†’ move β†’ verify
terraform state pull > backup-$(date +%s).tfstate
terraform state mv aws_vpc.main module.network.aws_vpc.main
terraform plan  # MUST show: No changes

Load references/error-recovery.md if already applied and resources were destroyed.

Module Version Hell

Signal: grep -r 'source.*?ref=' . | sort | uniq -c shows 3+ active versions.

Fix: Breaking changes require major version + 6-month deprecation + migration guide. Or eliminate versions entirely with monorepo workspace protocol.

Module Boundaries

Where to draw the boundary?
β”‚
β”œβ”€ By lifecycle β†’ GOOD (VPC rarely changes vs EC2 often changes)
β”œβ”€ By team ownership β†’ GOOD (clear responsibility)
β”œβ”€ By technology type β†’ BAD ("database module" cuts across concerns)
└─ By resource type β†’ BAD (aws_vpc module alone loses cohesion)

Good: VPC + subnets + route tables + NAT gateway (one cohesive networking unit) Bad: Just VPC (consumer must wire subnets manually)

Prefer composition (small focused modules wired together) over monolithic (one module creates everything). Exception: compliance modules that must enforce standards together.

Refactoring Checklist

  1. grep -r "resource \"aws_s3_bucket\"" . β€” confirm 3+ usages before touching anything
  2. diff app1/s3.tf app2/s3.tf β€” confirm >80% identical, not superficially similar
  3. Design interface around intent (bucket_type = "data"|"logs"|"artifacts"), not resource args
  4. terraform state pull > backup.tfstate β€” always before state moves
  5. terraform state mv <old-address> <new-address> β€” one resource at a time
  6. terraform plan β€” must show "No changes" before proceeding

When to Load References

Load references/error-recovery.md when:

  • State migration already applied and caused destroy/recreate
  • Module has grown to 10+ boolean toggles and needs redesign
  • Multiple module versions causing maintenance coordination problems

Do NOT load for:

  • Basic modularization decisions (use Core Decision tree above)
  • Single resource state moves (use Refactoring Checklist above)
  • Terraform syntax help (see official docs)

Arguments

$ARGUMENTS: Optional user-provided target, path, environment, symptom, or constraint. When empty, infer the narrowest safe scope from the current repository context and ask only if multiple high-impact choices remain.

Source: SKILL.md on GitHub

1 warning5mo5 checks Β· Risk SAFE
  • Gen Agent Trust Hub6mo

    The skill provides a decision-making framework and best practices for refactoring Terraform code into modules. It includes standard administrative shell commands for state migration. No malicious patterns or security risks were identified.

  • Socket6mo

    No alerts

  • Snyk6mo

    Risk: LOW Β· No issues

  • Runlayer6mo

    1/3 files flagged

  • ZeroLeaks5mo

    Score: 93/100 Β· 2 sections analyzed

Signed by skilld at 9d099e9. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 4 months ago

README badge

README badge for acedergren/agentic-tools/refactor-module