Summary
In this chapter, we introduced Terraform modules. Modules are at the heart of IaC as they are the key component to reusing code quickly and efficiently. The structure of a module follows the same structure we have been using to write Terraform code. We declare variables in variables.tf
, place our main code in main.tf
, and use the outputs.tf
file to expose information to the root module.
However, it’s important to use modules wisely. A module should not be a thin wrapper to existing resource definitions. A well-designed module adds value by providing a layer of abstraction. This can include provisioning multiple resources in a single module and adding organizational patterns and policies. In our albeit simple example, we provisioned not only a server but also a static IP address, and we added a layer of abstraction by providing a mapping of simple server size to instance types.
When you start writing Terraform code, always think about modules. Local modules help...