You inherit a module nested five levels deep and nobody can follow it. What do you do?
AdvancedScenarioModules and Code Structure
Answer
Situation read → Over-abstraction.
What I'd do →
- Map the real resources with
terraform state listandterraform graph. - Flatten to at most two levels: root calling modules calling resources.
- Use
movedblocks while collapsing wrappers so state stays untouched. - Verify with
terraform planshowing no changes.
Why → Deep wrappers pass variables through layers without adding value, and every layer is another place for a bug.
Interview tip → Refactor behind a no-diff plan; that's your safety net.
More on Modules and Code Structure
When do you turn a block of resources into a module, and when do you leave it alone?BeginnerHow do you consume a community module like `terraform-aws-modules/vpc/aws` responsibly?BeginnerAn ECS service module needs the VPC and subnets created by a network module. How do you wire them together?IntermediateHow would you lay out a repo that deploys the same stack to dev, stage and prod?IntermediateHow do you release a shared module so consumers can upgrade at their own pace?IntermediateA module needs to deploy to a second region. Why does putting a provider block inside the module cause trouble?Advanced