Modules you can reuse
Terraform · 12 questions
Short notes
Key idea
A module is a folder of Terraform files used like a function: inputs (variables) in, resources built, outputs out. Modules stop you copying the same blocks for every team or environment.
Syntax
module "sales_lake" {
source = "./modules/lake"
name = "sales"
location = "asia-south1"
keep_days = 365
}
output "raw_bucket" { value = module.sales_lake.raw_bucket_name }
- The module folder has
variables.tf,main.tf,outputs.tf. - Sources: local paths, Git URLs (
?ref=v1.2.0), the Terraform Registry (withversion). terraform initafter adding or changing a module source.
Example
A lake module creates a raw bucket, a curated bucket, a dataset and the IAM for one domain. Calling it three times (sales, marketing, finance) builds three identical lakes from 15 lines.
Traps
- Pin module versions; an unpinned Git source can change under you.
- Too many tiny modules make code hard to follow; modularise repeated groups, not single resources.
- Moving resources into a module changes their addresses; use
movedblocks to avoid recreating them. - Expose only the outputs callers need.
Want the full lesson with step-by-step examples, a project and a certificate? Terraform course, ₹120 →
- Question 1easy
What is a Terraform module?
- Question 2medium
Which blocks become a module's inputs?
- Question 3easy
In a
moduleblock, what doessourcedo? - Question 4medium
You add a new
moduleblock and runterraform plan. It fails with "Module not installed". What do you run? - Question 5hard
You need the bucket's URL from inside a module, but the module has no output for it. What's right?
Modules you can reuse
Modules you can reuse
Modules you can reuse
Modules you can reuse
Modules you can reuse
7 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.