This chapter covers
- Reusable components through the use of modules
- The three different types of variables in Terraform: local, input, and output
- How Terraform handles typing
- Validating inputs for more resilient code
- Dealing with sensitive data such as passwords
- Variable files and workspaces
One of the big promises of infrastructure as code is reusability. Being able to write something once and then allowing it to be used throughout a project has a huge number of benefits. Developers can use components they aren’t as familiar with, experts can expand and configure the components in the best possible way even as needs grow, and everyone can see how the system is being built. Terraform promotes reusability through the use of modules.
Modules are different from providers in a few ways. Modules are pure Terraform HashiCorp Configuration Language (HCL), so there’s no Go provider. In fact, a module cannot define new types or resources itself and instead uses the resources and data sources provided by individual providers. Modules do not have to be vendor specific—they can incorporate resources from multiple providers. While providers expose resources that are extremely low-level, mapping to a specific infrastructure component, modules can be used to create higher-level abstractions over systems composed of multiple components.