Writing Reusable Code Using Modules
One of the main objectives of Infrastructure as Code (IaC) is to create reusable code using the Don’t Repeat Yourself (DRY) software principle. Andrew Hunt and David Thomas first formulated this principle (Hunt, Andrew; Thomas, David (1999). The Pragmatic Programmer: From Journeyman to Master (1 ed.). USA: Addison-Wesley. pp. 320. ISBN 978-0201616224). In essence, it states that we should avoid repeating code.
In functional programming languages, we use functions to keep our code DRY. In Terraform, we use modules. In this chapter, we will start by outlining the basic structure of a module and then see how to write flexible modules that will be useful to others in our organization or the community at large. We will then describe how to use private and public Terraform registries to share code in your organization or with the world.
Thus, by the end of this chapter, you will have learned how to write Terraform modules that you (and others...