Modules
Written by
Updated at August 7, 2025
Modules in Terraform are a code organization tool that allows you to break down a larger infrastructure into smaller, manageable parts.
A module is a separate file with code describing a specific part of your infrastructure. It may contain resources, variables, input parameters, and other elements you need to create and manage infrastructure.
Terraform modules offer multiple benefits:
- Code reuse. You can use modules across different projects, which eliminates code duplication and streamlines development.
- Code organization. Splitting infrastructure into modules improves code organization and readability.
- Testing. You can test each module independently, which simplifies debugging and improves infrastructure reliability.
To create a module, follow these steps:
- Create a new
.tfor.tf.jsonfile. - Define input parameters for calling the module.
- Describe resources and configurations the module should create.
- Call the module from the main configuration file using the
modulecommand.
Module use case:
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}
In this example, we call the vpc module located in the ./modules/vpc file. The module accepts the cidr_block input parameter defining the IP address range for the cloud network.