3 Terraform variables and modules

 

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.

3.1 Modules

 
 

3.1.1 Module usage

 
 

3.1.2 Module file structure

 
 

3.1.3 Root module

 
 
 

3.1.4 Submodules

 

3.1.5 Module registries

 

3.2 Input, output, and local variables

 
 

3.3 Input variables

 
 
 

3.3.1 Defining and using inputs

 
 

3.3.2 Marking variables as sensitive

 

3.3.3 Using types for robust code

 
 
 

3.4 Outputs

 
 
 

3.4.1 Defining and using outputs

 
 
 

3.4.2 Sensitive outputs

 
 

3.4.3 Output dependencies

 
 
 

3.5 Locals

 

3.5.1 Defining and using locals

 

3.6 Value types

 
 
 

3.6.1 Strings

 
 

3.6.2 Numbers

 
 
 

3.6.3 Booleans

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest