linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Knowledge base
  • Cheat sheet
  • Capstone
  • Interview prep
home/terraform/kb/Refactoring

kb/refactoring

Terraform refactoring: moved, removed, import

Terraform refactoring with moved, removed, and import: the `moved` block moves a resource into a module without recreating it. The `removed` block drops it from state but keeps it in the cloud. The `import` block brings an existing resource under management. This is declarative refactoring without `state mv` by hand and without destroy and recreate.

  • tf-refactor-patternsRefactoring patterns: count to for_each, split files, extract module

    Large configs turn into spaghetti. The core refactoring patterns are: count to for_each (stable keys), splitting files by domain (network/compute/storage), extracting a repeated block into a module, merging small resources into a composite one, and removing dead imports. Each pattern is covered step by step, with a `plan` check at every step.

  • tf-removed-blockremoved block: drop a resource from state, keep it in the cloud

    `removed { from = ..., lifecycle { destroy = false } }` tells Terraform declaratively: remove this resource from management, but do not touch it in the cloud. The block was introduced in TF 1.7 and replaces the manual `terraform state rm` command. With `destroy = true` it behaves like an ordinary resource deletion from HCL.

  • tf-moved-blockThe moved block: rename without destroy

    `moved { from = ..., to = ... }` in HCL declaratively tells Terraform: "this resource used to live at one address and now lives at another, the cloud object is the same." The plan shows a "move", not a "destroy + create". It arrived in TF 1.1. It replaces the manual `terraform state mv`, leaves a trace in git, repeats for everyone on the team, and shows up in the diff.

Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies