Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
Variables & Outputs
- Input Variables
- Variable Types
- Default Values
- Environment Variables
- Output Values
- Variable Validation
State Management
- Terraform State File
- Terraform Remote State
- Terraform State Locking
- Terraform Drift Detection
- Terraform Refresh
- Terraform Import
Modules (Reusability)
- Terraform Modules
- Terraform Public Modules
- Terraform local modules
- Terraform Module Versioning
- Terraform Nested Modules
Provisioners & Lifecycle
ποΈ Terraform Local Modules: Custom Reusable Blocks
In modern DevOps and cloud infrastructure, reusability and standardization are key to managing complexity. Terraform provides a powerful feature for this: π Local Modules β your own custom reusable blocks.
While public modules are downloaded from the Terraform Registry, local modules are created and stored within your own codebase. They encapsulate related resources (like networks, VMs, IAM roles, or storage) into one logical, reusable unit.
This allows teams to reduce duplication, enforce best practices, and accelerate deployments across environments.
πΉ What Are Terraform Local Modules?
A Terraform Local Module is a self-contained directory that defines a set of resources, variables, and outputs which can be imported and used in multiple Terraform configurations within the same repository or organization.
Itβs like creating your own library of infrastructure building blocks.
π‘ Simple Definition:
Local Modules are reusable Terraform code units stored locally to organize and standardize your infrastructure.
πΉ Benefits of Using Local Modules
Benefit | Description |
---|---|
Reusability | Write once, use across multiple environments. |
Maintainability | Centralize updates to resources (like tags, naming conventions). |
Scalability | Modularize infrastructure for teams or environments. |
Consistency | Enforce uniform standards for cloud resources. |
Simplicity | Reduces Terraform file size and improves readability. |
πΉ How Local Modules Work
Explanation: The root module references multiple local modules for different components (network, compute, security). Each module has its own variables and outputs, but they work together as one infrastructure.
πΉ Folder Structure for Local Modules
project/ββββ main.tfβββ variables.tfβββ outputs.tfβββ modules/ βββ vpc/ β βββ main.tf β βββ variables.tf β βββ outputs.tf βββ compute/ β βββ main.tf β βββ variables.tf β βββ outputs.tf βββ storage/ βββ main.tf βββ variables.tf βββ outputs.tf
πΉ Local Module Anatomy
Each module typically includes:
main.tf
β Resource definitionsvariables.tf
β Input parametersoutputs.tf
β Outputs to export values back to root
You can call a local module using:
module "example" { source = "./modules/example"}
πΉ Example 1: AWS VPC Local Module
modules/vpc/main.tf
resource "aws_vpc" "this" { cidr_block = var.cidr tags = { Name = var.name }}
modules/vpc/variables.tf
variable "cidr" {}variable "name" {}
modules/vpc/outputs.tf
output "vpc_id" { value = aws_vpc.this.id}
main.tf (root)
provider "aws" { region = "us-east-1"}
module "vpc_main" { source = "./modules/vpc" cidr = "10.0.0.0/16" name = "my-main-vpc"}
output "main_vpc_id" { value = module.vpc_main.vpc_id}
β Result: Creates a reusable VPC using a local module. You can reuse it in multiple environments (dev, test, prod) by changing variables only.
πΉ Example 2: Azure Storage Account Module
modules/storage/main.tf
resource "azurerm_storage_account" "this" { name = var.name resource_group_name = var.resource_group_name location = var.location account_tier = "Standard" account_replication_type = "LRS"}
modules/storage/variables.tf
variable "name" {}variable "location" {}variable "resource_group_name" {}
main.tf (root)
provider "azurerm" { features {}}
module "storage" { source = "./modules/storage" name = "tfstorage001" location = "East US" resource_group_name = "tf-rg"}
β Result: Creates an Azure Storage Account using a local module β easily portable across projects.
πΉ Example 3: GCP Compute Instance Module
modules/compute/main.tf
resource "google_compute_instance" "vm" { name = var.instance_name machine_type = var.machine_type zone = var.zone
boot_disk { initialize_params { image = var.image } }
network_interface { network = var.network access_config {} }}
modules/compute/variables.tf
variable "instance_name" {}variable "machine_type" {}variable "zone" {}variable "image" {}variable "network" {}
main.tf (root)
provider "google" { project = "gcp-project-demo" region = "us-central1"}
module "vm_instance" { source = "./modules/compute" instance_name = "tf-vm-1" machine_type = "e2-medium" zone = "us-central1-a" image = "debian-cloud/debian-11" network = "default"}
β Result: Deploys a GCP Compute Instance using a custom module β reusable for multiple zones or machine types.
πΉ Advantages of Local Modules
Advantage | Description |
---|---|
Custom-fit for your Org | Designed to match your internal standards. |
Reusable within Repo | Used across multiple stacks easily. |
Version Control Ready | Managed with Git for traceability. |
No Internet Dependency | Works offline without Terraform Registry. |
Fast Iteration | Instant testing and updates. |
πΉ Best Practices for Local Modules
-
β Use Clear Naming Conventions Example:
modules/network
,modules/security
,modules/app
-
π¦ Define Variables & Outputs Clearly Avoid hardcoding; make your module flexible.
-
π Use Version Control (Git) Keep module versions in sync with your code.
-
π§© Keep Modules Focused One module = one purpose (e.g., VPC, EC2, IAM).
-
π§± Document Usage Add
README.md
with variable descriptions and usage examples.
πΉ How to Remember This Concept
Use the mnemonic βLOCALβ:
Letter | Meaning |
---|---|
L | Logical structure for your infra |
O | Organize reusable components |
C | Create consistency |
A | Avoid duplication |
L | Leverage modular architecture |
π‘ Memory Trick:
βLOCAL Modules keep your infrastructure Local, Organized, Consistent, and Always reusable.β
πΉ Interview & Exam Preparation
πΈ Common Interview Questions
-
Q: What is a local module in Terraform? A: A reusable infrastructure component defined within the same Terraform project.
-
Q: How do you reference a local module? A:
module "example" {source = "./modules/example"} -
Q: Whatβs the difference between local and public modules? A:
- Local β created and stored locally.
- Public β downloaded from Terraform Registry.
-
Q: Can you version-control local modules? A: Yes, by managing them through Git branches or commits.
πΈ Study Technique β βR.A.P.I.D.β
Step | Action |
---|---|
R | Read official Terraform docs |
A | Apply examples manually |
P | Practice creating modules for each cloud |
I | Inspect open-source module structures |
D | Document your own modules |
πΉ Why Itβs Important to Learn Local Modules
1. Foundation for Reusable Infrastructure
They form the basis for building enterprise-grade IaC systems.
2. Helps in Terraform Certification Exams
Questions about module structure, usage, and dependency handling are common in HashiCorp Certified: Terraform Associate exam.
3. Accelerates Team Development
Teams can share internal module libraries for rapid cloud provisioning.
4. Supports Multi-Cloud Infrastructure
Local modules can be replicated across AWS, Azure, and GCP.
5. Promotes Best Practices
Encourages clean, DRY (Donβt Repeat Yourself) Terraform code.
πΉ Common Mistakes to Avoid
Mistake | Why Itβs Bad | Solution |
---|---|---|
Hardcoding variables | Reduces reusability | Use variables.tf |
Not outputting values | Canβt reference module results | Define outputs |
Mixing multiple roles | Confusing module logic | Keep modules small and focused |
Not testing modules | Can cause dependency failures | Test locally with sample configs |
πΉ Real-World Use Cases
-
Enterprise Infrastructure Templates
- Standardize AWS VPC, IAM roles, and tagging using local modules.
-
Multi-Environment Setup
- Use the same module for Dev, Staging, and Production with different variable values.
-
Microservice Architecture
- Each service has its own compute, networking, and storage modules.
-
Hybrid Cloud Solutions
- Combine local modules across AWS, GCP, and on-prem resources.
πΉ Comparison Table: Local vs. Public Modules
Feature | Local Module | Public Module |
---|---|---|
Source | Local directory | Terraform Registry |
Usage Scope | Internal to repo | Global, open-source |
Internet Access | Not required | Required |
Customization | Full control | Limited by module design |
Security | Private | Publicly accessible |
πΉ Summary Table
Topic | Key Takeaway |
---|---|
Definition | Locally defined reusable Terraform components |
Syntax | source = "./modules/<name>" |
Purpose | DRY, maintainable, reusable IaC |
Examples | AWS VPC, Azure Storage, GCP Compute |
Mnemonic | LOCAL = Logical, Organized, Consistent, Always reusable |
Exam Tip | Practice referencing local modules |
Benefit | Promotes modular, scalable design |
Terraform Local Modules are the backbone of modular infrastructure as code (IaC). They enable developers and DevOps teams to build reusable, organized, and maintainable cloud infrastructure components β without rewriting the same code.
By mastering local modules, you can:
- Simplify your infrastructure management
- Build reusable architectures
- Prepare effectively for Terraform certification
π¬ βThink modular β build once, deploy everywhere.β
With hands-on practice and consistent naming, youβll turn your Terraform projects into elegant, scalable blueprints ready for enterprise automation.