Building modules
Note
The code for this section is under the chap04/local-module
directory in the GitHub repo of this book.
A module in Terraform serves the same purpose as a function in traditional programming languages. A module is a self-contained chunk of code that can be called repeatedly to create cloud infrastructure. Actually, we have already created modules. The Terraform configuration we wrote and executed is known as a root module. Modules called from other modules and stored locally are also known as child modules. So the root module can call a child module, which can call other modules.
The basic structure of a module is simple. By convention, we have three files: main.tf
, which contains the main code, variables.tf
, which defines the variables that need to be passed to the module, and outputs.tf
, which contains the information passed back to the calling module. Do remember Terraform itself only cares about file extensions, here, .tf
. Thus, as far as Terraform...