advanced 6h

Terraform Reusable Modules

Build a module library with remote state and workspaces.

0%

0 of 6 steps

What you will end up with

A versioned module library

Multi-environment state

A clean module interface

Build steps

0/6

Look for a resource block, like a storage bucket setup, that is already copy-pasted across two or more environments. If you are pasting the same recipe twice, it is begging to become a cookbook page.

Move those resources into their own folder, like modules/storage, with its own main.tf. Extracting is just moving that recipe out of one giant binder and onto its own labeled card.

Add variable blocks for anything that changes per use, and output blocks for anything callers will need afterward. Inputs are the ingredients list, outputs are what the recipe hands back once it is done.

Tag the module’s repo or path with something like v1.0.0 and reference that tag wherever it is called. A version tag is a best-by date, so callers know exactly which recipe they are getting.

Call the same module from both staging and production configs, passing different variable values for each. Same recipe, different oven settings depending on the kitchen.

Add validation blocks on variables, like checking a CIDR format, so bad inputs fail fast with a clear message. It is a taste test before the dish leaves the kitchen, not after it has already been served.

Before you start

Modules & reuseWorkspaces & environments