Module Overview

In Terraform, a module is a directory that holds a collection of configuration files. Such configuration files use either .tf or .tf.json extensions and define the basic elements of a module, including resources, data sources, outputs, and variables.
Modules are a way of grouping and encapsulating associated infrastructure components, which makes your Terraform configurations modular, maintainable, and reusable. They group related resources together so that managing and applying changes to the infrastructure much easier.
Modules can also be nested, meaning they can contain other modules inside themselves. This creates a tree-like structure, where one top-level module may include many child modules, each responsible for a certain aspect of the infrastructure. That provides nested setups so you can divide your infrastructure into smaller fragments that are easy to understand, maintain, and collaborate on with your Terraform configurations.

Why does Module need to be used in configuration?

Reusability: Module encourages the reusage of codes whereby one will be able to declare once an infrastructure component and then reuse it in several of his projects. That saves time and effort, plus gives consistency across your infrastructure.
Abstraction: Modules abstract the implementation details, allowing users to use a module in terms of what it does rather than in terms of how it is accomplished. This makes the configuration easier to use and more comprehensible to the user.
Organization: Modules break down complex configurations into smaller, more manageable units that makes them easier to understand, maintain, and test. This becomes especially true in large-scale infrastructure.
Collaboration: Modules can enable sharing and reuse of infrastructure components across projects within teams by supporting collaborative work on modules. Consistency is facilitated at the same time, without duplication, to allow faster development of new infrastructure.

Types of modules

There are two major types of modules :

1. Root Module

Every Terraform configuration requires at least one module, which is called a Root Module. A root module serves as the entry point for your Terraform configuration and defines your infrastructure.
The root module is a group of resources defined in the .tf files in your working directory. It can also include calls to other modules, which makes it easier to split your Terraform configurations into units that can be reused.

2. Child Module

The root module, or any other module, may call one or more other modules whose resources are to be added to the final infrastructure. A module called by a root module has been described as a child module.
Child modules can be called from the root module or any other module several times, which means that with this one is able to reuse the same module in different parts of your infrastructure.

Location of Child Modules

The Child components can be in one of three locations:

1. Local file system

The child module is directly stored within your Terraform project's directory or anywhere else in your local file system. Hence, you can easily access and modify the source code of the module as needed.
When you want to use a child module in your root module (the main Terraform configuration file) or any other module, you need to inform Terraform where to locate the child module code. You do this through the source argument inside the module block in the Terraform configuration file. The source argument takes a relative path to the location of the child module.
Now, for instance, assume that you have a child module named ec2_module and you need to use the same in your root module. You can specify the path or location of the child module using an argument called source as mentioned below.
module "ec2_module" {
    source = "../../modules/ec2-configuration"
}
In this example, the source argument specifies the local location in which the module is located in your file system. Terraform will then import all the module code from the ../../modules/ec2-configuration into the root module or other module.

2. Public registry

Terraform supports storing and using child modules in a public registry, which is a centralized repository of community-contributed modules. One such example of a public registry is the Terraform Registry by HashiCorp. The Terraform Registry is a large repository of modules contributed by the Terraform community for nearly every type of infrastructure-related functionality. That way, it would be easy for one to find and incorporate prebuilt modules with ease in their Terraform configuration file using the Terraform Registry without actually having to write the code.
When including one module from the Terraform Registry into your Terraform configuration, you need to specify the URL of the Terraform Registry via the source argument and the name of the required module via local_name. In this way, Terraform will know exactly which module it should download from the registry.

Example: Using a Module from the Terraform Registry

For example, if you want to use a module called aws-s3-bucket from the Terraform Registry in your Terraform configuration, the source for this module should look like the following:
module "s3-bucket" {
    source = "registry.terraform.io/terraform-aws-modules/iam/aws"
    version = "4.1.2"
}
In this example,
When you execute the command terraform init, Terraform will download the version 4.1.2 of the s3-bucket module from the terraform-aws-modules/iam/aws location in the Terraform Registry (registry.terraform.io) and prepare it to be used within your Terraform configuration.

3. Private registry

A private registry is where customized modules are stored, which are not publicly available. It's a secure way for organizations to host and manage their internal, proprietary Terraform modules that solve organization-specific problems.
Private registries are a necessity when your Terraform modules contain sensitive or confidential information that should not be shared outside your organization. This could include:
By hosting these modules within a private registry, you are ensuring that they are not accessible to all and reducing the possibility of unauthorized access or data breaches.

Related Pages

Feedback

Was this page helpful?