Modules
Last updated
Last updated
The capability to generate reusable infrastructure components known as modules is one of the primary characteristics that contribute to Terraform power.
A Terraform module is a collection of standard configuration files in a dedicated directory. Terraform modules encapsulate groups of resources dedicated to one task, reducing the amount of code you have to develop for similar infrastructure components.
Reusability
Code Reduction
Organization
Consistency
Parameterization
Management
Resources Vs Modules
A resource in Terraform describes a piece of infrastructure that is going to be created (e.g., a VPC, a subnet, an ec2 instance, etc).
A module is a collection of resources that are used together to achieve a reusable use case.
Terraform Modules Workflow
A typical module would resemble this:
Root Module
Child Module
Local Module
Published Module
Within your primary Terraform setup, you can call and instantiate modules using the modules block.
Example
Root Module
The root module consists of all the resources defined in the .tf files in a Terraform configuration, meaning that all Terraform configurations have their own root module.
Even if you are simply creating a main.tf that has just a locals block inside of it with a local variable, that is still considered a root module.
Child Module
Every module can call other modules, and all of the modules called inside another module are considered child modules.
Calling a child module means to include all the resources defined in that module in the current configuration. This is done by using a module block inside your Terraform configuration:
Local Module
A local module is a module that wasn’t published in any registry and when it is sourced, it is using the path to that particular module.
Published Module
A published module refers to a module that has been pushed to a Terraform Registry, or even simply on a VCS and has a tag associated with it. When a published module is sourced, the URL of that module is used either from the registry or from the VCS itself