Root Module
The root module is the main Terraform configuration directory where Terraform commands are executed.
Key Topics
- • Main configuration
- • Provider configuration
- • Module calls
- • Infrastructure orchestration
Learn how Terraform modules help you organize, reuse, and manage infrastructure configurations using modular Infrastructure as Code.
Terraform Module Flow
Create Reusable Module
Define Module Variables
Create Infrastructure Resources
Expose Module Outputs
Call Module from Root Configuration
Introduction
Terraform modules allow you to group related infrastructure resources into reusable components.
Instead of keeping every resource inside one large Terraform configuration, modules allow you to separate infrastructure into smaller logical components.
A module can represent a web server, network, Kubernetes cluster, database, or any other reusable infrastructure component.
Core Concepts
These concepts form the foundation for building reusable and well-organized Terraform infrastructure.
The root module is the main Terraform configuration directory where Terraform commands are executed.
Key Topics
Child modules are reusable Terraform configurations that can be called from a root module or another module.
Key Topics
Input variables allow a module to receive configuration values from the module that calls it.
Key Topics
Output values allow a module to expose useful information to the root module or other parts of the configuration.
Key Topics
Module Structure
A typical Terraform project can contain a root module and one or more child modules.
Each module can contain its own resources, variables, and outputs inside a dedicated directory.
Example Project Structure
terraform-project/
│
├── main.tf
├── variables.tf
├── outputs.tf
│
└── modules/
│
└── web-server/
├── main.tf
├── variables.tf
└── outputs.tfChild Module Resource
resource "aws_instance" "web_server" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = var.server_name
Environment = var.environment
}
}Module Resources
A child module can contain one or more Terraform resources just like a normal Terraform configuration.
The difference is that the module can receive values through input variables, making the infrastructure reusable across multiple deployments.
Module Variables
Modules use input variables to receive values from the root module or another calling module.
This allows one module to support different configurations without modifying the internal resource definitions.
Module Input Variables
variable "ami_id" {
description = "AMI ID for the server"
type = string
}
variable "instance_type" {
description = "EC2 instance type"
type = string
}
variable "server_name" {
description = "Name of the server"
type = string
}
variable "environment" {
description = "Deployment environment"
type = string
}Calling a Terraform Module
module "web_server" {
source = "./modules/web-server"
ami_id = "ami-example"
instance_type = "t3.micro"
server_name = "web-server"
environment = "development"
}Module Blocks
A Terraform module is called using a module block. The source argument tells Terraform where the module is located.
The root module can provide values for the child module input variables when calling the module.
Module Outputs
Modules can expose useful resource information using Terraform output blocks.
The root module can reference these outputs using the module name followed by the output name.
Module Output Example
output "instance_id" {
description = "ID of the web server"
value = aws_instance.web_server.id
}
output "public_ip" {
description = "Public IP address"
value = aws_instance.web_server.public_ip
}Benefits
Create infrastructure components once and reuse them across multiple environments and projects.
Modules help divide large Terraform configurations into smaller and easier-to-manage components.
The same module can be used to create consistent infrastructure across development, staging, and production.
Infrastructure changes can often be managed centrally within a reusable module.
Complete Example
The root module can call a child module, provide input values, and use output values from the created infrastructure.
# Root Module
module "web_server" {
source = "./modules/web-server"
ami_id = "ami-example"
instance_type = "t3.micro"
server_name = "production-web"
environment = "production"
}
output "web_server_ip" {
value = module.web_server.public_ip
}Learning Roadmap
Learn how Terraform uses modules to organize and reuse infrastructure configurations.
Create a dedicated directory containing the Terraform resources for a reusable component.
Add input variables that allow the module to receive configuration values.
Define the infrastructure resources that the module will manage.
Expose useful infrastructure information using output values.
Use a module block from the root configuration to create infrastructure from the reusable module.
Best Practices
Each module should have a clear purpose and manage related infrastructure components.
Use descriptive input variable names that clearly explain the configuration being provided.
Document the purpose, variables, outputs, and expected usage of reusable modules.
Create output values for infrastructure information that other modules or configurations may need.
Use input variables to make modules flexible and reusable across environments.
Manage and track changes carefully when modules are reused across multiple infrastructure projects.
Continue Learning
Learn the foundations of Terraform and Infrastructure as Code.
⚙️Learn how Terraform providers connect with infrastructure platforms and manage resources.
📦Learn how to create flexible Terraform configurations using input variables and output values.
Next Step
Practice organizing Terraform resources into modules, defining module variables, exposing outputs, and reusing the same infrastructure components across multiple environments.