
A Terraform module is a collection of configuration files that encapsulate resources used together to achieve a specific outcome. Modules promote reusability, organization, and maintainability in infrastructure as code by allowing you to group related resources and manage them as a single unit.
Understanding the Module Block
In Terraform, you define a module block to incorporate the contents of another module into your configuration. This allows you to reuse existing configurations and standardize resource provisioning across your infrastructure.
Syntax of a Module Block
A typical module block has the following structure:
module "<MODULE_NAME>" {
source = "<SOURCE>"
version = "<VERSION>"
# Additional arguments corresponding to the module's input variables
}
<MODULE_NAME>
: A unique identifier for the module instance within your configuration.<SOURCE>
: Specifies the location of the module’s source code. This can be a local path, a Git repository, or a Terraform Registry address.<VERSION>
: Defines the version of the module to use, applicable when sourcing modules from registries.
Example: Using a Module from the Terraform Registry
Suppose you want to deploy an AWS Virtual Private Cloud (VPC) using a community-maintained module from the Terraform Registry. You can define the module block as follows:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.19.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-west-1a", "us-west-1b", "us-west-1c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
enable_nat_gateway = true
tags = {
Terraform = "true"
Environment = "dev"
}
}
In this example:
source
: Specifies the module’s location in the Terraform Registry.version
: Ensures that Terraform uses version 3.19.0 of the module, maintaining consistency across deployments.- The subsequent arguments (
name
,cidr
,azs
, etc.) correspond to the input variables defined by the module, allowing customization of the VPC’s configuration.
Example: Using a Local Module
You can also create and reference local modules within your project directory. Assume you have a module that sets up an AWS EC2 instance, located in the modules/ec2-instance
directory:
module "web_server" {
source = "./modules/ec2-instance"
instance_type = "t2.micro"
ami_id = "ami-0c55b159cbfafe1f0"
subnet_id = "subnet-abc12345"
}
Here:
source
: Points to the relative path of the local module.- The arguments (
instance_type
,ami_id
,subnet_id
) are input variables defined within theec2-instance
module, allowing you to customize the EC2 instance’s properties.
Key Components of a Module
A well-structured Terraform module typically includes the following files:
main.tf
: Contains the primary resource definitions.variables.tf
: Declares input variables to parameterize the module.outputs.tf
: Defines output values to expose information about the resources.versions.tf
: Specifies the required Terraform version and provider constraints.README.md
: Provides documentation on the module’s purpose and usage.
Best Practices for Using Modules
- Encapsulation: Modules should encapsulate their resources, exposing only necessary inputs and outputs.
- Reusability: Design modules to be reusable across different configurations and environments.
- Versioning: Implement version control for modules to manage changes and ensure stability.
- Documentation: Provide clear documentation within the module directory to explain its purpose and usage.
- Consistency: Use consistent naming conventions and file structures across modules.
By effectively utilizing module blocks, you can create modular, reusable, and maintainable infrastructure configurations, enhancing the scalability and manageability of your Terraform projects.
$ tree minimal-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
resource "azurerm_resource_group" "example" {
name = "my-resources66"
location = "West Europe"
}
module "apache"{
source = "./modules/install_apache"
}
module "nginx"{
source = "./modules/install_nginx"
instances = ["${module.web.instance_ids}"]
}
module "network" {
source = "Azure/network/azurerm"
resource_group_name = azurerm_resource_group.example.name
address_spaces = ["10.0.0.0/16", "10.2.0.0/16"]
subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
subnet_names = ["subnet1", "subnet2", "subnet3"]
subnet_service_endpoints = {
"subnet1" : ["Microsoft.Sql"],
"subnet2" : ["Microsoft.Sql"],
"subnet3" : ["Microsoft.Sql"]
}
tags = {
environment = "dev"
costcenter = "it"
}
depends_on = [azurerm_resource_group.example]
}
key parameters available for a Terraform module
block
Certainly! Here’s a table summarizing the key parameters available for a Terraform module
block:
Parameter | Description | Required | Example |
---|---|---|---|
source | Specifies the location of the module’s source code. This can be a local path, a Git repository, or a Terraform Registry address. | Yes | source = "./modules/network" or source = "terraform-aws-modules/vpc/aws" |
version | Defines the version of the module to use, particularly when sourcing modules from registries. | No | version = "3.0.0" |
providers | Overrides the default provider configurations for the module. Useful when you need to specify different provider settings for a particular module. | No | providers = { aws = aws.us_east } |
count | Creates multiple instances of the module. Allows you to scale resources by specifying the number of instances. | No | count = 3 |
for_each | Creates multiple instances of the module based on a map or set of strings. Provides more control compared to count , especially when each instance requires unique configurations. | No | for_each = { net1 = "10.0.0.0/16", net2 = "10.1.0.0/16" } |
depends_on | Specifies dependencies on other resources or modules. Ensures that the module is provisioned only after certain resources or modules have been created. | No | depends_on = [aws_vpc.main] |
*Note: Input variables are user-defined parameters that allow customization of the module’s behavior. Each input variable must be defined within the module’s variables.tf
file. When calling the module, you provide values for these variables.*
By utilizing these parameters, you can effectively manage and customize the behavior of your Terraform modules, leading to more modular and maintainable infrastructure configurations.
Terraform Module Block Source Arguments Style










I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND