Blog 17

What are Terraform Modules?

A module is a container for a collection of related resources. Modules may be used to construct lightweight abstractions so that your infrastructure can be described in terms of its architecture rather than real things. When you run terraform plan or terraform apply, the.tf files in your working directory come together to form the root module. That module has the ability to call other modules and link them by transferring output values from one module to the input values of another.

What does a Terraform Module do?

You may use a Terraform module to construct logical abstraction on top of a resource collection. In other words, a module enables you to aggregate resources and reuse them later, maybe several times. Module blocks can also be used to enforce compliance on other resources, such as databases that utilise encrypted drives. By hard-coding the encryption settings rather than exposing it through variables, you ensure that the disks are encrypted every time the module is used.

Modules are commonly used to create and manage many resources with a simplified interface. This reduces the amount of duplicate code in a project significantly. Copying and pasting portions of code around while just altering a few parameters might be laborious. For example, instead of establishing each unique resource required for a fully functional VPC, if charged with constructing many VPCs for different environments, a single VPC module may be invoked multiple times.

Why should you use Terraform Modules?

There are a lot of reasons why you may want to create and muse Terraform modules. One of the biggest reasons you want to use Terraform Modules is because it allows you to use reusable code, and also cut down the time it takes to normally put together a terraform file. It could a file that had 100+ lines all the way down to about 20 lines. It makes a big difference and allows the user to review and look over the code at a glance, rather than forcing the user to scroll through lines of code.