Terraform modules help you avoid repeating code and make your Infrastructure as Code (IaC) reusable, scalable, and maintainable.
What is a Terraform Module
A Terraform module is a collection of .tf files that are grouped together to perform a specific task.
Think of a module like a Java java Terraform
Class Module
MethodParameters Variables
Return Values Outputs
Object Creation. Module Call
Instead of writing the same EC2 code multiple times, you create a module once and reuse it everywhere.
Why Use Modules?
Without Modules
resource "aws_instance" "dev" {
ami = "ami-123456"
instance_type = "t2.micro"
}
resource "aws_instance" "test" {
ami = "ami-123456"
instance_type = "t2.micro"
}
resource "aws_instance" "prod" {
ami = "ami-123456"
instance_type = "t2.micro"
}
Problem:
Duplicate code
Hard to maintain
Error-prone
With Modules
module "dev" {
source = "./modules/ec2"
instance_name = "dev-server"
}
module "test" {
source = "./modules/ec2"
instance_name = "test-server"
}
module "prod" {
source = "./modules/ec2"
instance_name = "prod-server"
}
Benefits:
Reusable
Cleaner code
Easy maintenance
Standardization
Project Structure
terraform-project/
│
├── main.tf
│
└── modules/
└── ec2/
├── main.tf
├── variables.tf
└── outputs.tf
Step 1: Create Module
modules/ec2/main.tf
resource "aws_instance" "server" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = var.instance_name
}
}
modules/ec2/variables.tf
variable "ami_id" {
description = "AMI ID"
}
variable "instance_type" {
description = "EC2 Type"
}
variable "instance_name" {
description = "Server Name"
}
modules/ec2/outputs.tf
output "instance_id" {
value = aws_instance.server.id
}
output "public_ip" {
value = aws_instance.server.public_ip
}
Step 2: Call Module
Root main.tf
provider "aws" {
region = "us-east-1"
}
module "webserver" {
source = "./modules/ec2"
ami_id = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
instance_name = "dev-webserver"
}
Step 3:InitializeTerraform
terraform init
Output:
Initializing modules...
webserver in modules/ec2
Terraform downloads and prepares the module.
Step 4: Validate
terraform validate
Output:
Success! The configuration is valid.
Step 5: Plan
terraform plan
Output:
aws_instance.server
Terraform shows resources that will be created.
Step 6: Apply
terraform apply
Terraform creates:
EC2 Instance
Tags
Networking attachments
Step 7: Access Module
Outputs
Add to root:
output "instance_ip" {
value = module.webserver.public_ip
}
Apply again:
terraform apply
Output:
instance_ip = 54.x.x.x
Real-Time Enterprise Example
VPC Module
modules/vpc
Creates:
VPC
Public Subnets
Private Subnets
Route Tables
Internet Gateway
EC2 Module
modules/ec2
Creates:
EC2 Servers
Security Groups
RDS Module
modules/rds
Creates:
MySQL Database
DB Subnet Group
Root Module
module "vpc" {
source = "./modules/vpc"
}
module "ec2" {
source = "./modules/ec2"
subnet_id = module.vpc.public_subnet_id
}
module "rds" {
source = "./modules/rds"
subnet_ids = module.vpc.private_subnet_ids
}
Architecture:
Root Module
|
+-- VPC Module
|
+-- EC2 Module
|
+-- RDS Module
Best Practices
1. One Module = One Responsibility
Good:
ec2 module
vpc module
rds module
Bad:
everything module
2. Use Variables
Avoid hardcoding:
instance_type = var.instance_type
3. Expose Only Required Outputs
output "instance_id"
Avoid exposing unnecessary values.
4. Version Control Modules
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
}
Simple Interview Questions
What is a Terraform Module?
A reusable collection of Terraform configurations used to create infrastructure components.
What is the difference between Root Module and Child Module?
Root Module → Main Terraform execution directory.
Child Module → Reusable module called by the root module.
How are values passed into modules?
Using input variables.
Hcl
instance_type = "t2.micro"
How do modules return values?
Using outputs.
Hcl
module.ec2.public_ip
Key Takeaway
Terraform Modules are the foundation of enterprise Infrastructure as Code. They promote reusability, standardization, scalability, and maintainability. In large DevOps environments, teams typically create separate modules for VPC, EC2, EKS, RDS, IAM, Security Groups, and Load Balancers, then assemble them through a root module to build complete cloud platforms.
Top comments (0)