Use of Modules
Imagine you’ve built a Lego set (your Terraform module) and placed it neatly in a box. Now comes the fun part: using it to build your city. In Terraform, this means calling modules from your root configuration, passing them inputs, and receiving outputs.
Modules aren’t just theoretical but they’re the reusable engines that power real infrastructure. This chapter teaches you how to plug modules into your projects and make them work for you.
Calling a Module
sourcedefines where the module lives:- Local path (
./modules/ec2) - GitHub repo (
git::https://github.com/user/repo.git) - Terraform Registry (
terraform-aws-modules/vpc/aws)
- Local path (
Modules are invoked with the module block:
module "web" {
source = "./modules/ec2"
}
Passing Inputs
- Modules accept variables defined in their
variables.tf. - Inputs make modules flexible and reusable across environments.
module "web" {
source = "./modules/ec2"
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
instance_name = "web-server"
}
Consuming Outputs
- Modules expose values via
outputs.tf. - Outputs allow chaining modules together (e.g. pass VPC ID from one module to another).
output "web_instance_ip" {
value = module.web.public_ip
}
Local vs Remote Modules
- Local modules: Stored in your project folder.
- Remote modules: Pulled from Terraform Registry, GitHub, or private repos.
- Remote modules are versioned and shareable across teams.
Hands‑On Lab / Demo
Lab: Using a Local EC2 Module
- Create
modules/ec2withmain.tf,variables.tf, andoutputs.tf. - Run:
terraform init→ initializes module.terraform apply→ provisions EC2 instance.- Observe: Terraform prints the public IP from module output.
In root main.tf:
module "web" {
source = "./modules/ec2"
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
instance_name = "web-server"
}
output "web_ip" {
value = module.web.public_ip
}
Lab: Using a Remote VPC Module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b"]
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnets = ["10.0.3.0/24", "10.0.4.0/24"]
}
- Run
terraform init→ downloads module from Registry. - Outputs include VPC ID, subnet IDs, etc.
Pro Tips & Best Practices
- Always pin module versions when using remote sources.
- Document inputs/outputs for clarity.
- Keep module calls clean - avoid passing too many variables.
- Use outputs to chain modules together (e.g., VPC → EC2 → Security Groups).
- Test modules in isolation before using them in production.
Summary & Cheatsheet
- Call modules with
moduleblock. - Pass inputs via variables.
- Consume outputs with
module.<name>.<output>. - Sources: Local paths, GitHub, Terraform Registry.
Quick mnemonic: Call → Input → Output → Source
The Hackers Notebook
Using modules is where Terraform becomes powerful and scalable. Instead of repeating code, you call reusable building blocks, pass them inputs, and consume outputs to connect your infrastructure. With this skill, you can build complex systems with clarity and efficiency.
