Lesson 4 · Terraform fundamentals
Modules
Write infrastructure once, instantiate it everywhere — the DRY unit that makes an env×org matrix possible.
Your win: explain what a Terraform module is, how inputs/outputs parameterize it, and why this repo's 34 modules are the reusable building blocks behind ~180 per-cell configs.
Don't write the cluster config 30 times
You need a GKE cluster in stag-manabie, prod-tokyo,
prod-jprep, … Copy-pasting the same 200 lines of cluster resources into each cell
is the env×org problem all over again (Course 1). A module is the fix: a
reusable, parameterized package of resources you write once and instantiate with
different inputs.1 It's the same "define once, stamp
out many" idea as a Helm library chart (Course 3), but for cloud resources.
Inputs and outputs — the module's interface
- Input variables (
variable) — the module's parameters (project, region, node-pool sizes). The caller passes values. - Outputs (
output) — values the module returns (the cluster's name, a bucket's URL, a service account's email). Callers read them, and one module's output feeds another's input.
# calling a module module "platforms" { source = "../../..//modules/platforms" project_id = var.project_id gke = local.gke_config # the input } # … later, another component reads its output namespace = module.platforms.gke_identity_namespace
dependency blocks
(Lesson 5).
platforms (GKE + Cloud SQL + KMS + GCS),
apps (service accounts + Workload Identity), vpc,
kms-key, github-oidc, cloudflare-dns, … (Lesson 7). None
of them create anything on their own — they're definitions. The ~180
terragrunt.hcl leaves under live/ are thin: each
just points at a module and passes that cell's values
(terraform { source = "…/modules/platforms" }). One module definition; many
instantiations across the env×org matrix. That ratio — 34 defs → 180 instances — is exactly
why modules matter.
modules/platforms/gke.tf
wraps terraform-google-modules/kubernetes-engine). You rarely write raw resources
for a GKE cluster — you configure a well-tested module. Standing on others' modules is normal
and good; know where the registry ones end and yours begin.
Terraform — Modules
Authoring and calling modules, input variables, outputs, and composition.
Check yourself (from memory)
Q1. A Terraform module is…
Q2. One module's output feeding another's variable creates…
Q3. In this repo, the live/ leaves are thin because…
variable INPUTS (caller passes values) + output RETURNS (caller
reads; one module's output → another's input = a DEPENDENCY Terraform orders, e.g. VPC before
GKE). Call with module "x" { source = "…/modules/x"; ... }. Modules often WRAP
community modules (repo GKE wraps terraform-google-modules). REPO: 34 module defs under
modules/ (platforms/apps/vpc/kms-key/github-oidc/…) → ~180 thin
terragrunt.hcl LEAVES under live/, each pointing at a module with that
cell's values. That 34→180 ratio is why modules matter.