MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Terraform Tutorials: Module Complete Guide

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
}
Code language: PHP (php)

  • <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"
  }
}
Code language: JavaScript (javascript)

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"
}
Code language: JavaScript (javascript)

Here:

  • source: Points to the relative path of the local module.
  • The arguments (instance_type, ami_id, subnet_id) are input variables defined within the ec2-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]
}
Code language: JavaScript (javascript)

key parameters available for a Terraform module block

Certainly! Here’s a table summarizing the key parameters available for a Terraform module block:

ParameterDescriptionRequiredExample
sourceSpecifies the location of the module’s source code. This can be a local path, a Git repository, or a Terraform Registry address.Yessource = "./modules/network" or source = "terraform-aws-modules/vpc/aws"
versionDefines the version of the module to use, particularly when sourcing modules from registries.Noversion = "3.0.0"
providersOverrides the default provider configurations for the module. Useful when you need to specify different provider settings for a particular module.Noproviders = { aws = aws.us_east }
countCreates multiple instances of the module. Allows you to scale resources by specifying the number of instances.Nocount = 3
for_eachCreates 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.Nofor_each = { net1 = "10.0.0.0/16", net2 = "10.1.0.0/16" }
depends_onSpecifies dependencies on other resources or modules. Ensures that the module is provisioned only after certain resources or modules have been created.Nodepends_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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Terraform Tools for Developer Productivity Master Guide

Tools, Workflow, Security, Testing, CI/CD, Automation, AI Assistance, Governance, and Production Best Practices Verified baseline: 5 September 2026Audience: Terraform developers, DevOps/SRE, platform/cloud engineers, module authors, DevSecOps teams, and engineering…

Read More

AWS EKS: How to enable Auto Mode using Terraform?

Let’s clarify the reality for July 2026: EKS “Auto Mode” vs. “Karpenter Auto Compute” in Terraform 1. The Real EKS Auto Mode (Serverless) 2. cluster_compute_config in terraform-aws-modules/eks…

Read More

Kubernetes: Deploying AWS Load Balancer Controller with Terraform

Absolutely, deploying the AWS Load Balancer Controller (ALB Controller) in production via Terraform is now a very common, robust, and recommended practice. There are official Terraform modules…

Read More

Terraform source for Modules from Git: All Patterns & Examples

Here’s a comprehensive tutorial on all valid Terraform module “source” Git URL patterns, with clear examples (no Drivemode references), so you can master using modules from GitHub,…

Read More

What is Terrafile?

Terrafile is a tool used to manage Terraform modules as dependencies. It simplifies the process of downloading and managing Terraform modules by automating the fetching of modules…

Read More

Automating Infrastructure with Terraform: Best Practices and Use Cases

As the complexity of managing IT infrastructure continues to grow, automation has become an indispensable part of modern DevOps practices. Terraform, an open-source infrastructure as code (IaC)…

Read More