Table of contents
- What are modules in Terraform and why do we need modules in Terraform?
- What are the benefits of using modules in Terraform?
- Create/Define a module in Terraform to encapsulate reusable infrastructure configuration in a modular and scalable manner.
- Module composition:
- Module versioning:
- What are the ways to lock Terraform module versions?
What are modules in Terraform and why do we need modules in Terraform?
A Terraform module is a set of Terraform configuration files in a single directory. Even a simple configuration consisting of a single directory with one or more .tf files is a module. When you run Terraform commands directly from such a directory, it is considered the root module.
Every Terraform configuration is part of a module. You may have a simple set of Terraform configuration files such as:
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.
What are the benefits of using modules in Terraform?
Reusability
Scalability
Team Collaboration
Create/Define a module in Terraform to encapsulate reusable infrastructure configuration in a modular and scalable manner.
First, we need to create a separate directory for this then we need to create an EC2 instance in AWS for that need to create insta_data.tf.
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
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 prov_data.tf.
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.
Module composition:
In a simple Terraform configuration with only one root module, we create a flat set of resources and use Terraform's expression syntax to describe the relationships between these resources:
When we introduce
module
blocks, our configuration becomes hierarchical rather than flat: each module contains its own set of resources, and possibly its child modules, which can potentially create a deep, complex tree of resource configurations.Module versioning:
When using modules installed from a module registry, we recommend explicitly constraining the acceptable version numbers to avoid unexpected or unwanted changes.
Use the
version
argument in themodule
block to specify versions:
What are the ways to lock Terraform module versions?
When you initialize a Terraform configuration for the first time with Terraform 1.1 or later, Terraform will generate a new .terraform.lock.hcl
file in the current working directory. You should include the lock file in your version control repository to ensure that Terraform uses the same provider versions across your team and in ephemeral remote execution environments.
Open the .terraform.lock.hcl
file.
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.
Thank you for Reading!!!!!!!