Introduction
You have to trust me if I say you already write modules.
Even when you don't create a module intentionally, if you use Terraform, you are already writing a module – a so-called "root" module. Any Terraform configuration file (.tf) in a directory, even just one, forms a module.
What Is a Terraform Module?
Suppose you have some area to build a house. You went to an architect asking for a blueprint of your dream house. If the architect is helpful enough, you can build a house using that drawing.
Now after some days one of your friends came to you asking for that blueprint to build his own house. He also has the same build area as you have. Using the same blueprint he can also build a house like you.
The same thing happens in Terraform. Suppose you have to build an infrastructure which has a VPC, and 4 subnets and within that subnet you should have 3 instances, and they should be assigned to some security groups. Now to build that you wrote around 300 lines of code. After some days, for another application, you got an assignment to write the same infrastructure. So again you have to write around 300 lines of code, which is repetitive. Terraform module helps you here.
Terraform makes it easier to grow your infrastructure and keep its configuration clean. But, as the infrastructure grows, a single directory becomes hard to manage. That’s where Terraform modules come in.
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.
Why do we need modules in Terraform?
--Using modules can save time and reduce costly errors by re-using configurations written either by yourself, other members of your team, or other Terraform practitioners who have published modules for you to use.
--Usually, new projects contain a small number of resources and files. But, after some releases and improvements, more resources and files are added to the projects. If you do not use a module it will be really difficult to manage complex projects.
--Duplication will become a problem.
--Suppose you decided to update one configuration because you found one issue. Later you noticed that the same error occurs in different environments, like production, Dev, and QA. So, you will need to review all parts and make changes to fix one issue. Instead of that, if you use a module you just need to change one file and it will solve the issue.
By using modules, you can easily create, reuse, and maintain infrastructure configurations, promoting code reusability, abstraction, and scalability.
Create a module in Terraform
Step 1: Create a Directory for Your Module
The first step is to create a directory for your module. You can do this in your terminal using the mkdir command, followed by the name of your module directory.
mkdir terraform-module
Step 2: Define the Module Inputs and Outputs
The next step is to define the inputs and outputs for your module. Inputs are variables that the user of the module can set to configure it, while outputs are values that the module returns to the user.
Create a new file named variables.tf within the "terraform-module" directory, and define the input variables for your module using the "variable" block.
Here’s an example:
Next, create another file named outputs.tf within the "terraform-module" directory, and define the output values for your module using the output block. Here's an example:
Step 3: Define the Resources
The next step is to define the resources that your module creates. Resources are the actual infrastructure components that Terraform manages.
Create a new file named main.tf within the "terraform-module" directory, and define the resources for your module. Here's an example:
Step 4: Add Provider Configuration
If your module uses resources from a specific cloud provider, you’ll need to configure the provider. Create a new file named providers.tf within the directory, and add the provider configuration. Here's an example:
This module creates a sample EC2 instance in AWS.
Initialize and apply the configuration: Run terraform init to initialize the configuration and download any required dependencies. Then, run terraform apply to apply the configuration and create the resources defined in the module.
Modular Composition
Composition is a collection of infrastructure modules, which can span across several logically separated areas (e.g., AWS Regions, several AWS accounts). Composition is used to describe the complete infrastructure required for the whole organization or project.
A composition consists of infrastructure modules, which consist of resources modules, which implement individual resources.
By composing modules, you can build complex infrastructure configurations by combining smaller, reusable modules. This promotes code reuse, simplifies management, and allows for better organization and abstraction of your infrastructure code. Module composition also facilitates the creation of infrastructure as code that is more modular, maintainable, and scalable.
Suppose you have two separate modules: a networking module and a compute module. To compose these modules, you would create a new Terraform configuration file (e.g., main.tf
) that references both modules and defines how they should interact:
In this example, the main.tf file includes both the network and compute modules. You provide the necessary input variables for each module, such as the VPC CIDR block and the number of instances, to customize their behaviour.
Module Versioning
Module versioning in Terraform allows you to track changes to your modules over time and specify the version of a module to use. It ensures consistency and control over module updates. It also ensures that infrastructure deployments are predictable and can be rolled back if needed.
In the above image, I have specified the module versions, by mentioning that you can control when to adopt new versions and avoid unexpected changes in your infrastructure deployments.
What are the ways to lock Terraform module versions?
The Terraform Lock File is always named .terraform.lock.hcl
and is located in the current working directory where Terraform is run. This is because the lock file is created to track the compatible dependency versions for the entire Terraform Project, rather than having separate lock files for each module in the project. This working directory is also the “root” directory of the Terraform Project itself.
The Terraform Lock File is named .terraform.lock.hcl
with the .terraform
prefix in it that matches the name of the .terraform
directory. The .terraform
directory is a hidden directory which is used by Terraform to cache provider plugins and remove modules dependencies referenced by the Terraform project code.
When terraform init
command is run, it will automatically create the Terraform Lock File if it doesn’t exist. If the file already exists, then Terraform will update it with the latest dependency versions selected. It is recommended that the lock file be included in version control repositories with the rest of the Terraform (.tf
) files for the project.
Github Link:
https://github.com/SudipaDas/TerraWeek.git
If this post was helpful, please follow and click the 💚 button below to show your support.
_ Thank you for reading!
_Sudipa