automationJuly 21, 2026•5 min read
Terraform Modules - Building Reusable Infrastructure Components | DevOps Duoo
In this tutorial, you'll learn how to build reusable infrastructure components using Terraform modules. You'll discover how to structure your modules fo...
D
DevOps Duoo
DevOps Expert
Terraform Modules - Building Reusable Infrastructure Components
TL;DR
- Learn how to create reusable infrastructure components using Terraform modules, following best practices for modular and efficient infrastructure as code.
- Understand how to structure, publish, and consume Terraform modules, including the use of the Terraform Module Registry.
- Master the art of composing complex infrastructure topologies from smaller, reusable modules, ensuring scalability, maintainability, and security.
What You'll Learn
In this tutorial, you'll learn how to build reusable infrastructure components using Terraform modules. You'll discover how to structure your modules for maximum reusability, how to publish and consume modules from the Terraform Module Registry, and how to compose complex infrastructure topologies from smaller, modular pieces. By the end of this tutorial, you'll be able to create efficient, scalable, and secure infrastructure as code using Terraform modules.Creating Terraform Modules
Terraform modules are self-contained packages of Terraform configuration that can be reused across multiple projects. To create a Terraform module, you'll need to create a new directory for your module and add amain.tf file that defines the infrastructure components included in the module.Module Structure
A typical Terraform module consists of the following files:main.tf: The main entry point for the module, which defines the infrastructure components included in the module.variables.tf: A file that defines the input variables for the module, which can be used to customize the module's behavior.outputs.tf: A file that defines the output values for the module, which can be used by other modules or by the root Terraform configuration.
# File: main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-abc123"
instance_type = "t2.micro"
}# File: variables.tf
variable "instance_type" {
type = string
default = "t2.micro"
description = "The type of instance to create"
}# File: outputs.tf
output "instance_id" {
value = aws_instance.example.id
description = "The ID of the created instance"
}
To use this module, you would create a new Terraform configuration that includes the module, like this:
# File: main.tf
module "example" {
source = file("./path/to/module")
instance_type = "t2.small"
}
This would create a new EC2 instance with the specified instance type.Publishing and Consuming Modules
Once you've created a Terraform module, you can publish it to the Terraform Module Registry, which allows you to share your module with others and easily consume modules created by others.To publish a module, you'll need to create a new repository on a Git hosting platform like GitHub or GitLab, and then use the Terraform registry command to publish your module to the registry.
Here's an example of how to publish a module to the Terraform Module Registry using the Terraform CLI:
# Initialize a new Git repository for your module
git init
# Add your module files to the repository
git add .
# Commit your changes
git commit -m "Initial commit"
# Create a new repository on your Git hosting platform
# ...
# Publish your module to the Terraform Module Registry
terraform registry publish \
--module-name example-module \
--module-version 1.0.0 \
--git-repo https://github.com/example/example-module.git
Once your module is published, others can consume it by including it in their Terraform configurations, like this:
# File: main.tf
module "example" {
source = "example/example-module/aws"
version = "1.0.0"
instance_type = "t2.small"
}
This would create a new EC2 instance using the example-module module.Composing Complex Infrastructure Topologies
One of the key benefits of using Terraform modules is that they allow you to compose complex infrastructure topologies from smaller, reusable pieces. This makes it easier to manage and maintain your infrastructure, and reduces the risk of errors and inconsistencies.To compose complex infrastructure topologies using Terraform modules, you can create a hierarchy of modules, where each module depends on one or more other modules. For example:
# File: main.tf
module "network" {
source = file("./network-module")
}
module "database" {
source = file("./database-module")
vpc_id = module.network.vpc_id
}
module "application" {
source = file("./application-module")
database_host = module.database.host
}
This would create a new network, database, and application infrastructure, where each component depends on the previous one.Common Mistakes and Troubleshooting
When working with Terraform modules, there are several common mistakes to watch out for, including:- Module version conflicts: When using multiple modules that depend on different versions of the same underlying infrastructure components, you may encounter version conflicts. To avoid this, make sure to specify explicit version constraints for each module.
- Circular dependencies: When composing complex infrastructure topologies, you may encounter circular dependencies between modules. To avoid this, make sure to structure your modules in a hierarchical manner, where each module depends on one or more other modules.
- Module output errors: When using module outputs, you may encounter errors if the output values are not correctly defined or if they are not properly referenced in the consuming module. To avoid this, make sure to define explicit output values for each module and to reference them correctly in the consuming module.
Key Takeaways
- Terraform modules allow you to build reusable infrastructure components that can be easily shared and consumed across multiple projects.
- To create effective Terraform modules, follow best practices for modular and efficient infrastructure as code, including structuring your modules for maximum reusability and using explicit version constraints.
- When composing complex infrastructure topologies using Terraform modules, make sure to structure your modules in a hierarchical manner and to avoid circular dependencies.
- To troubleshoot common issues with Terraform modules, see for more information.
- For more information on Terraform and infrastructure as code, see and .