(Day 42) Task : Terraform Modules: Reusability & Scalability in Infrastructure as Code :-

DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS
What is a Terraform Module?
A Terraform Module is a container for multiple resources that are used together. It is simply a directory containing .tf files. Think of it like a function in programming: instead of repeating the same code, you define it once and reuse it with different inputs.
In simpler terms:
A module is a reusable block of Terraform configuration that can be called with different variables and environments.
“Write once, use everywhere” – that’s the power of Terraform Modules!
Why Use Terraform Modules?
Here are some key benefits of using Terraform modules:
| Benefit | Explanation |
| Reusability | Write infrastructure once and use it across environments (dev, staging, prod). |
| Scalability | Manage complex infrastructure by breaking it into smaller, manageable units. |
| Consistency | Avoid copy-paste errors. Maintain uniform standards. |
| Versioning | Modules can be versioned and tracked in a source control system like Git. |
| Collaboration | Teams can work on different modules independently. |
Anatomy of a Terraform Module :-
A typical Terraform module directory may contain:
terraform-aws-ec2/
│
├── main.tf # Main resources
├── variables.tf # Input variables
├── outputs.tf # Output values
├── README.md # Documentation (recommended)
Even your main Terraform configuration is technically a module – the root module.
How to Use a Module :-
Step-by-Step Usage Example:
Let’s say you have a module to create an AWS EC2 instance.
1. Define the Module
Folder structure:
modules/
└── ec2/
├── main.tf
├── variables.tf
└── outputs.tf
modules/ec2/main.tf :
resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
tags = {
Name = var.name
}
}
modules/ec2/variables.tf :
variable "ami" {}
variable "instance_type" {}
variable "name" {}
modules/ec2/outputs.tf :
output "instance_id" {
value = aws_instance.example.id
}
2. Call the Module in Your Main Code :-
module "my_ec2" {
source = "./modules/ec2"
ami = "ami-0abcd1234efgh5678"
instance_type = "t2.micro"
name = "dev-instance"
}
3. Apply Terraform :-
terraform init
terraform apply
You’ve now used your EC2 module to spin up a reusable instance!
Remote Modules from GitHub or Terraform Registry :-
You can even source modules from:
Terraform Registry:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "4.0.2"
name = "my-vpc"
cidr = "10.0.0.0/16"
}
GitHub:
module "s3" {
source = "git::https://github.com/yourorg/terraform-aws-s3.git?ref=v1.0"
bucket_name = "my-dev-bucket"
}
Real-Life Use Case: Dev/Stage/Prod Environments
You can call the same module with different values:
module "dev_ec2" {
source = "./modules/ec2"
ami = "ami-dev123"
instance_type = "t2.micro"
name = "dev-server"
}
module "prod_ec2" {
source = "./modules/ec2"
ami = "ami-prod456"
instance_type = "t3.medium"
name = "prod-server"
}
Best Practices for Using Modules :
| Practice | Why it Matters |
Use input validation (type, description, default) | Prevent errors with unexpected variable types |
| Name your outputs clearly | So they can be used easily in parent modules |
| Keep modules small & focused | One resource type or concern per module |
| Version control your modules | So you can rollback or upgrade confidently |
Write README.md files | Helps teams understand how to use the module |
Use terraform-docs tool | To auto-generate documentation |
Debugging Common Issues Faced :-
Modules not found?
Check if you ranterraform init.Variables not passed?
Ensure you define required variables or assign defaults.Output not available?
Double-checkoutputs.tfand that your module applied successfully.



