Struggling with Terraform Module Reusability โ€“ Want Advice

Hey everyone,

I have been working with Terraform & I am trying to clean up my setups by breaking things into modules. It is going okay but I have hit a few bumps. :sweat_smile:

I am dealing with passing tons of variables into modules & it is getting a bit messy. I am not sure if I should split modules further or just rethink how I am organizing everything. Also, I keep going back and forth on when to use locals vs variables โ€” any simple rule of thumb?

I followed a Python tutorial that emphasized keeping code DRY and modular & it made me realize I want to level up my Terraform game too.

Also i have check this Using terraform-google-org-policy module still want advice. If anyone has a good structure they follow or any best practices for naming, module layout or managing outputs cleanly, I want to hear it.

Thank you.:slight_smile:

I tend to use locals for a few things

  • Mangling / restructuring data internally within a module
  • Things that you want to define centrally or want to DRY up a bit, but donโ€™t necessarily want to document with tf-docs or allow a user to modify.

Setting sensible defaults is one other way that you can avoid needing to pass too many variables in, but I guess it all depends on what youโ€™re trying to do, and how variable your use cases are.

Using data structures (like a map or object) can be another way to manage resources via modules without too many different parameters, but that can have its own pitfalls at times, and youโ€™ll likely want to invest time into figuring out how to validate them properly.

ps - if you post a snippet / sanitized example (wrapped in a code block) of your variables and how youโ€™re using them, maybe folks would have some other suggestions.