Learning Outcome
5
Recognize modules for scalable infrastructure
4
Review infrastructure created with modules
3
Understand module execution during deployment
2
Use modules in root configuration
1
Understand Terraform modules for infrastructure deployment
Let’s recall what we already learned
Terraform modules and how they help reuse infrastructure code
How modules use variables and outputs to pass information between configurations
How Modules help organize infrastructure into reusable and manageable components
Infrastructure as Building Blocks
Imagine a construction company building multiple apartment buildings
Infrastructure as Building Blocks
Instead of designing every building from scratch, they reuse standard building blocks, such as:
Once these standard designs are created, they can be used repeatedly in different buildings
Electrical systems
Plumbing layouts
Elevator systems
From Construction Blocks to Terraform Modules
Similarly, in Terraform, modules allow us to deploy infrastructure using reusable configurations, making infrastructure deployment faster and more organized.
Deploying Infrastructure Using Modules
When using modules, the main Terraform configuration (root module)
calls reusable modules that contain infrastructure definitions
Root Module
Module
Infrastructure definitions
Module
Infrastructure definitions
Module
Infrastructure definitions
Deploying Infrastructure Using Modules
The deployment process works as follows:
The root configuration calls a module
The module contains resource definitions
Terraform processes the module and creates the required infrastructure
Modules allow teams to deploy standardized infrastructure components easily
Example: Deploying Infrastructure with a Module
Suppose we created a module that provisions an EC2 instance
module "web_server" {
source = "./modules/ec2"
instance_type = "t2.micro"
ami_id = "ami-123456"
}
Explanation:
module "web_server" → Name of the module instance
1
source → Location of the module
2
instance_type and ami_id → Values passed to module variables
3
Terraform reads the module and deploys the defined infrastructure
How Terraform Processes Modules
When Terraform runs the configuration:
Terraform loads the root configuration
It detects module blocks
Terraform loads module code from the specified source
Variables are passed from the root module to the child module
Terraform creates infrastructure resources defined inside the module
This allows Terraform to manage
complex infrastructure using reusable modules
Reviewing Infrastructure Using Modules
After deploying infrastructure, it is important to review and verify the deployed resources
Reviewing infrastructure helps ensure:
Resources were created successfully
Configuration matches the desired state
Infrastructure is functioning correctly
Methods to Review Infrastructure
1. Review Terraform Output
Terraform displays information such as:
These outputs help verify the deployed infrastructure
Resource IDs
Public IP addresses
DNS names
Methods to Review Infrastructure
2. Review Infrastructure in Cloud Console
You can check the resources directly in the cloud platform
Virtual machines
Storage resources
Networking components
This confirms that Terraform has successfully created the infrastructure
For Exmaple:
Methods to Review Infrastructure
3. Review Terraform State
Terraform keeps track of deployed resources in the state file
The state file stores:
Resource metadata
Stores details about each deployed resource
Current infrastructure state
Shows the current configuration of the infrastructure
Resource dependencies
Tracks how resources depend on each other
This helps Terraform understand the current infrastructure environment
Benefits of Deploying Infrastructure Using Modules
Standardized Infrastructure
Faster Deployment
Simplified Management
Improved Collaboration
Best Practices for Using Modules
Use clear module names
Keep modules small and focused
Use variables for configuration flexibility
Store reusable modules in central repositories
Use outputs to expose important resource information
Commands / Key Points
Module Block
Modules are called using the module block
Input Variables
Input values are passed using variables
Module Outputs
Outputs return important module information
Example Module Call
module "network" {
source = "./modules/vpc"
}
This configuration tells Terraform to use the VPC module
located in the modules directory
Summary
5
Modules help manage large infrastructure efficiently
4
Outputs return useful information from modules
3
Variables pass configuration values to modules
2
The root module calls child modules to provision infrastructure
1
Terraform modules enable reusable infrastructure deployment
Quiz
Which block is used to deploy infrastructure using a Terraform module?
A. provider
B. module
C. resource
D. variable
Quiz
Which block is used to deploy infrastructure using a Terraform module?
A. provider
B. module
C. resource
D. variable