Content ITV PRO
This is Itvedant Content department
Learning Outcome
5
Organize infrastructure using modules
4
Use input variables and outputs in modules
3
Reuse modules to reduce code duplication
2
Create a basic Terraform module
1
Understand Terraform Modules and their purpose
Recall
What Terraform components did we learn previously?
How does Terraform define infrastructure using code blocks and reusable values?
What happens when infrastructure grows and everything is managed in a single configuration file?
Imagine a construction company building multiple houses
Instead of designing every house from scratch, they reuse standard blueprints for common components such as:
These reusable blueprints help them build faster and maintain consistency
Kitchen layout
Bathroom structure
Electrical wiring
Similarly, Terraform provides Modules, which
allow us to reuse infrastructure configurations
instead of rewriting the same code multiple times
What is a Terraform Module?
A Terraform Module is a container for multiple Terraform configuration files that are used together to manage infrastructure
What is a Terraform Module?
Modules help in:
Organizing Terraform code
Reusing infrastructure components
Simplifying complex configurations
A module is a reusable infrastructure template
Why Modules are Needed
As infrastructure grows, Terraform code can become:
Large
Hard to manage
Difficult to maintain
Modules solve this problem by:
Improving code organization
Reducing duplicate configurations
Breaking infrastructure into smaller reusable components
Why Modules are Needed
Example reusable modules:
Database module
EC2 module
Security group module
VPC module
Types of Terraform Modules
It usually contains:
The root module is the main Terraform configuration directory where you run Terraform commands
Root Module
main.tf
variables.tf
outputs.tf
Types of Terraform Modules
Child modules are typically stored in:
A child module is a module that is called by another Terraform configuration
Child Module
Local directories
Remote repositories
Terraform module registry
Basic Structure of a Terraform Module
A typical module directory structure looks like this:
ec2-module
main.tf
Defines resources
variables.tf
Defines input variables
outputs.tf
Defines output values
Example: Creating a Simple Module
Define Module Resources
resource "aws_instance" "web_server" {
ami = var.ami_id
instance_type =
var.instance_type
}
Declare Resources to Create
Define Module Variables
variable "ami_id" {}
variable "instance_type" {}
Add input variable definations
Define Module Outputs
output "instance_id" {
value =
aws_instance.web_server.id
}
Specify output values to return
Using a Module
A module is used in the root configuration using the module block
module "ec2_instance" {
source = "./ec2-module"
ami_id = "ami-12345"
instance_type = "t2.micro"
}
module "ec2_instance"
source "./ec2-module"
ami_id & instance_type
Benefits of Terraform Modules
Code Reusability
Write infrastructure once and reuse it multiple times
Scalability
Modules help manage large infrastructure environments efficiently
Easier Maintenance
Changes can be made in one module and automatically applied wherever it is used
Better Organization
Infrastructure is divided into smaller logical components
Terraform Module Sources
Terraform modules can be stored in different locations
Common module sources include:
Local Path
source = "./ec2-module"
Git Repository
source = "git::https://github.com/example/ec2-module.git"
Terraform Registry
Modules can also be downloaded from the official registry
Key Points
Modules help reuse infrastructure code
Modules can be stored locally or remotely
Modules improve organization and scalability
Modules use variables and outputs for flexibility
Summary
5
Improve reusability, maintainability and scalability
4
Organize Terraform code into manageable components
3
Terraform supports root modules and child modules
2
Modules use variables for inputs and outputs for results
1
Terraform modules are reusable infrastructure configurations
Quiz
What is the main purpose of Terraform Modules?
A. To install Terraform
B. To reuse infrastructure code
C. To store state files
D. To run Terraform commands
Quiz-Answer
What is the main purpose of Terraform Modules?
A. To install Terraform
B. To reuse infrastructure code
C. To store state files
D. To run Terraform commands
By Content ITV