Skip to main content

Modules

Every company answers the same questions in its own repository. What is a region made of. What does a server need before it can run. What it takes to stand up a static site. The answers are good, they took real work, and they do not travel: copying a folder of Terraform from another team means taking on their account layout and their naming rules along with their code, because a module's contract with its caller is a list of strings and nothing checks them against your world.

A module is Octo's unit of reuse. It declares the inputs it accepts, builds models when it runs, and finds what other modules built by matching on shape rather than by importing their classes. Two modules from two libraries compose without either author having planned for it.

The shop is four of them: an app module, an account module, a region module, and a storage module, with a server module added later. Each one is a line in octo.yaml:

modules:
- moduleId: receipts-module
moduleClass: AwsS3StorageServiceModule
moduleInputs:
region: "${{region-module.model.region}}"
bucketName: "${env.RECEIPTS_BUCKET_NAME}"
accessPrefixes:
- uploads

The inputs are the contract​

A module declares what it accepts, and the declaration is checked before the module runs. region there is not a string that looks like a region ID. It is the region node itself, and the storage module's requirement is that whatever arrives publishes AWS region details, which is what lets it work out the account and region to put the bucket in without being told.

So a mistake in octo.yaml fails while modules are still composing, with a message naming the input, rather than three layers down while Terraform is being written. Optional inputs get their defaults at the same point. Validation covers the rules a module author can put on an input, and The Definition File covers writing the YAML.

A module that cannot satisfy what it was asked for stops there too. Ask the shop's server module for access to a prefix the receipts bucket does not have, and it says so before anything exists. The alternative is finding out partway through an apply with half a stack standing.

Order​

The moduleId is yours to pick. It names the folder of Terraform the instance generates, and everything the instance produces is recorded against it. Load the same class twice under two IDs and you get two independent instances, which is how the shop would get several subnets out of one subnet module.

Modules run in the order the definition file lists them, because a module can only be handed something an earlier one has already built: the region module comes before the storage module that is handed the region. Composing Modules covers the details. The order things are created in during an apply is a separate matter, decided by the dependencies between them.

Where a swap stops​

Everything one instance produces lands in one folder of generated Terraform, and that boundary is also the boundary of a replacement. Swap the shop's single-availability-zone region module for one that spreads across three, and what changes is the contents of region-module/. Nothing else in the definition file moves, as long as the new module publishes the same region anchor.

Composing Modules covers what that looks like in practice, and Create a Module covers writing one.