When building infrastructure with Terraform, copying and pasting the same code across projects quickly becomes messy.
Terraform Modules solve this by letting you write code once and reuse it anywhere—for dev, staging, production, or even multiple teams.
In this blog, you’ll learn:
- What Terraform Modules are
- How to create and use them
- Real-world examples and best practices
What Are Terraform Modules?
A module in Terraform is just a folder with Terraform configuration files (.tf
) that define resources.
- Root module → Your main project directory.
- Child module → A reusable block of Terraform code you call from the root module.
Think of modules as functions in programming:
- Input → Variables
- Logic → Resources
- Output → Resource details
Why Use Modules?
- Reusability → Write once, use anywhere.
- Maintainability → Fix bugs in one place, apply everywhere.
- Consistency → Ensure similar setups across environments.
- Collaboration → Share modules across teams.
Creating Your First Terraform Module
Step 1: Create Module Folder
terraform-project/
main.tf
variables.tf
outputs.tf
modules/
s3_bucket/
main.tf
variables.tf
outputs.tf
Step 2: Define the Module (modules/s3_bucket/main.tf)
variable "bucket_name" {
description = "Name of the S3 bucket"
type = string
}
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name
acl = "private"
}
output "bucket_arn" {
value = aws_s3_bucket.this.arn
}
Step 3: Call the Module in main.tf
module "my_s3_bucket" {
source = "./modules/s3_bucket"
bucket_name = "my-production-bucket"
}
Run:
terraform init
terraform apply
Terraform will create the S3 bucket using the module.
Using Modules from Terraform Registry
You can also use prebuilt modules:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.14.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
}
The Terraform Registry has official modules for AWS, Azure, GCP, and more.
Case Study: Multi-Environment Infrastructure
A startup had:
- Dev environment → Small resources
- Staging environment → Medium resources
- Production environment → High availability setup
They created one module for VPC, EC2, and S3:
- Passed environment-specific variables (instance size, tags).
- Reused the same modules for all environments.
Result: Reduced code duplication by 80%, simplified maintenance.
Best Practices for Modules
- Keep modules small → Each should focus on one task (e.g., S3, VPC).
- Version your modules → Tag releases in Git for stability.
- Use meaningful variables & outputs for clarity.
- Avoid hardcoding values always use variables.
- Document your modules so teams can reuse them easily.
Project Structure with Modules
terraform-project/
main.tf
variables.tf
outputs.tf
terraform.tfvars
modules/
s3_bucket/
main.tf
variables.tf
outputs.tf
vpc/
main.tf
variables.tf
outputs.tf
What’s Next?
Now you know how to:
-
Create your own modules
-
Reuse community modules
-
Build cleaner, scalable infrastructure
In Part 7, we’ll explore Workspaces & Environments to manage dev, staging, and prod in one Terraform project.
Bibliography
- HashiCorp. Terraform Workspaces Documentation. Available at: https://developer.hashicorp.com/terraform/language/state/workspaces
- HashiCorp. Managing Multiple Environments with Terraform. Available at: https://developer.hashicorp.com/terraform/tutorials/cli/workspaces
- AWS. Best Practices for Multi-Environment Deployments. Available at: https://docs.aws.amazon.com/whitepapers/latest/multi-environment-deployments/
- Microsoft Azure. Terraform and Azure DevOps Pipelines for Multiple Environments. Available at: https://learn.microsoft.com/en-us/azure/devops/pipelines/terraform
- Google Cloud Platform. Multi-Environment Infrastructure Management. Available at: https://cloud.google.com/docs/terraform