Why Do We Need Modules?
When you start using Terraform, you start by creating a few infrastructure objects by writing code in a single configuration file. As your requirements grow, you start adding additional configurations to the same file or adding new files to the same directory. Theoretically, you can even keep using this approach. However, is this the right approach that is recommended by HashiCorp?
Before answering this question, take a look at the drawbacks of this approach:
Complexity
When you start copy-pasting the code for similar requirements or multiple resources of the same configuration, the complexity (imagine having 50 blocks of code to create 50 instances) will increase with the growth in the number of resources. It becomes very difficult for the team managing Terraform to understand the code and navigate it for either changes or troubleshooting.
Duplication of Code
You use the same block of code with minor changes for similar resources. One of the...