Interview Prep/Terraform/Modules and Code Structure

You must refactor loose resources into a module in a live prod state without recreating anything. How?

AdvancedScenarioModules and Code Structure
Answer

Situation read → The resources already exist; only their addresses change.

What I'd do →

  1. Create the module with the same resource definitions.
  2. Add moved blocks from the root addresses to module.x.….
  3. Run terraform plan; it must show only "moved", no create/destroy.
  4. Apply, and keep the moved blocks for a release or two.
moved {
  from = aws_security_group.app
  to   = module.app.aws_security_group.this
}

Why → moved is declarative, reviewable and applies the same way in every environment.

Interview tip → Mention that plan output literally says "has moved to" for each, which is your proof.

All Terraform questions Roadmap: Terraform

More on Modules and Code Structure