Day 70- 90DaysOfDevOps
Terraform Modules

Hey Learners! Welcome back. We understand that by using Terraform we can provision our infrastructure by code using different blocks. Terraform, can benefit from the use of modules to simplify configuration maintenance. Let's dive deeper into Terraform Modules...
Terraform Modules:-
In Terraform, modules can be group resources that are used together, letting you break down a complex configuration into smaller, reusable components. Doing so will make a Terraform configuration easier to maintain and can reduce redundancy between multiple Terraform projects.
Modules can be reused in multiple parts of your Terraform configuration, promoting consistency and reducing code duplication.
Modules accept input variables and produce output values, making them customizable and composable.
How to use Module:-
Creating an EC2 Instance:-
First, create a new directory to define modules. (Below I have created an EC2 sub-directory in the module directory)
Similar to the main Terraform configuration root module, a child module should contain at least a main.tf, a variable.tf and an output.tf in the same directory(As we created earlier).

The main.tf file is used to define the main Terraform script. The variable.tf file defines all input parameters. The output.tf is used to define all the outputs.
Create all the .tf as follows (Make sure you created in module/EC2/ directory in this case.)
#main.tf
resource "aws_instance" "Module-Instance" {
ami = var.amiID
instance_type = var.instanceType
subnet_id = var.subnetID
}
#variable.tf
variable "amiID" {
description = "Value for the ami"
}
variable "instanceType" {
description = "Value for instance_type"
}
variable "subnetID" {
description = "Value for subnet_id"
}
#output.tf
output "PublicIP" {
value = aws_instance.Module-Instance.public_ip
}
Using a module:-
We wrote our module in the module/EC2 directory. The next step is to use the module.
Create main.tf in the parent folder where you created the module directory as shown in the below screenshot.

provider "aws" {
region = "ap-south-1"
}
module "EC2_module" {
source = "./module/EC2"
amiID = "" #as per requirements
instanceType = "t2.micro"
subnetID = "" #as per requirements
}
As shown, Module in Terraform have their keyword, so you can call the module using the module block and specify the location of the module using the source parameter.
Terraform supports several different source types, including Git, the Terraform Registry and HTTP URLs. We use a Local path.
Inside the module block, define the input parameters for the module with the following commands.
You can create additional resources in the same main.tf file as well.
1- Different Modules of Terraform
Here are the different modules of Terraform:-
Root Module:-
The Root Module is the entry point of your Terraform configuration. It's essentially the main configuration file that sets up your Terraform project.
The Root Module is the top-level configuration that initiates and orchestrates the provisioning of resources.
It often defines providers, declares variables, and calls child modules.
Child Module:-
A Terraform module usually a Root Module can call other modules to include their resources in the configuration. A module that has been called by another module is often referred to as a Child Module.
Child Modules allow you to abstract complex infrastructure into smaller, manageable units.
Published Module:-
A Published Module in Terraform refers to a module that has been shared with the community through the Terraform Module registry.
The Terraform Module registry is a public repository where users can share their modules with others to promote code reuse and collaboration.
Each module contains a set of resources and configurations that can be managed independently. This makes it easier to manage and test your infrastructure code and promotes code reuse across your organization. You can create your custom modules, or use existing modules from the Terraform Module Registry, which is a public repository of Terraform modules contributed by the community.
2- Difference Between Root Module and Child Module:-
The main difference between a root module and a child module in Terraform is their role in the overall configuration.
A root module is the top-level module of a Terraform configuration. It serves as the entry point for your entire Terraform configuration and typically includes your provider configuration and any variables that are needed to define your infrastructure resources. A root module is responsible for initializing Terraform, configuring providers, and declaring resources.
On the other hand, a child module is a sub-module that is nested within the root module or another child module. It is used to group related resources and configurations for easier management. A child module is typically created to encapsulate a set of resources that perform a specific function. Child modules are reusable components that can be used in multiple root modules.
Child modules can be defined within a root module using a module block, which specifies the source of the module and any input variables that need to be set. The source can be a local file path or a remote location, such as a version control system or a Terraform Module Registry.
Another difference between root modules and child modules is their visibility of variables. A root module can declare and reference variables that are visible to all child modules, while a child module can declare and reference variables that are only visible within that module.
3- Are Modules and Namespaces are same? Justify your answer for both Yes/No.
No, modules and namespaces are not the same concept, although they may share some similarities in certain contexts.
A module in Terraform is a self-contained unit of infrastructure code that encapsulates a set of related resources and can be reused across multiple configurations. Modules help to promote code reuse, modularity, and abstraction in Terraform, allowing users to manage complex infrastructures more easily.
On the other hand, a namespace is a way of organizing names or identifiers in a way that avoids naming conflicts. namespaces are commonly used in programming to avoid naming conflicts between different modules, functions, or variables, They provide a way to group related names under a common prefix or label.
While both modules and namespaces are used for organization and abstraction, they serve different purposes in different contexts. Modules in Terraform are used to organize and reuse infrastructure code, while namespaces are used to organize and avoid naming conflicts between different identifiers in programming.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!




