teluzia.Cloud Engineer · Day 93
CHAPTER 14 · ADVANCED TERRAFORM

Module design and reuse

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

A reusable module is a small appliance with a clear plug and controls; callers should not need to rewire its internals. A module interface should expose choices callers genuinely need while retaining safe defaults. Validate meaningful constraints and explain units. Too many unrelated toggles make behavior difficult to reason about; too few inputs force callers to copy and modify the module. Outputs provide selected module results. Callers should use outputs rather than depend on internal resource addresses. Encapsulation allows internal refactoring, but changes to output names, types, or meaning can break users and need a migration or versioned release.

Think of it this way

A reusable module is a small appliance with a clear plug and controls; callers should not need to rewire its internals.

TOPIC 01 / 03Step by step

Inputs, defaults, and validation

A module interface should expose choices callers genuinely need while retaining safe defaults. Validate meaningful constraints and explain units. Too many unrelated toggles make behavior difficult to reason about; too few inputs force callers to copy and modify the module. For example, a network module accepts CIDR and availability-zone choices, validates CIDR syntax, and describes subnet allocation expectations.

WORKED EXAMPLE
A network module accepts CIDR and availability-zone choices, validates CIDR syntax, and describes subnet allocation expectations.

A module author considers exposing thirty flags. Instead, start with the caller's real decisions: address space, environment name, required capacity, and supported exposure policy. Supply safe defaults for choices not normally made by callers, then validate constraints and document exceptions. Inputs should communicate intent rather than reproduce every provider attribute indiscriminately.

An input contract defines accepted values and meaning. Validation catches selected unacceptable combinations or values. A default is a deliberate fallback, not an excuse to omit documentation. Too many controls create a combinatorial testing burden; too few may force teams to fork the module. Include units, null behavior, and whether a change causes replacement. A reusable module cannot guarantee suitability for every organization. State its supported scope and provide examples that demonstrate actual assumptions, including cost and permissions, so callers can review the resulting resources rather than trust the package name.

Why this matters

A network module accepts CIDR and availability-zone choices, validates CIDR syntax, and describes subnet allocation expectations.

A common trap

Do not overlook this operational distinction: Too many unrelated toggles make behavior difficult to reason about; too few inputs force callers to copy and modify the module.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Inputs, defaults, and validation

A module interface should expose choices callers genuinely need while retaining safe defaults. Validate meaningful constraints and explain units. Too many unrelated toggles make behavior difficult to reason about; too few inputs force callers to copy and modify the module. For example, a network module accepts CIDR and availability-zone choices, validates CIDR syntax, and describes subnet allocation expectations.

WORKED EXAMPLE
A network module accepts CIDR and availability-zone choices, validates CIDR syntax, and describes subnet allocation expectations.

A module author considers exposing thirty flags. Instead, start with the caller's real decisions: address space, environment name, required capacity, and supported exposure policy. Supply safe defaults for choices not normally made by callers, then validate constraints and document exceptions. Inputs should communicate intent rather than reproduce every provider attribute indiscriminately.

An input contract defines accepted values and meaning. Validation catches selected unacceptable combinations or values. A default is a deliberate fallback, not an excuse to omit documentation. Too many controls create a combinatorial testing burden; too few may force teams to fork the module. Include units, null behavior, and whether a change causes replacement. A reusable module cannot guarantee suitability for every organization. State its supported scope and provide examples that demonstrate actual assumptions, including cost and permissions, so callers can review the resulting resources rather than trust the package name.

Why this matters

A network module accepts CIDR and availability-zone choices, validates CIDR syntax, and describes subnet allocation expectations.

A common trap

Do not overlook this operational distinction: Too many unrelated toggles make behavior difficult to reason about; too few inputs force callers to copy and modify the module.

Outputs and encapsulation

Outputs provide selected module results. Callers should use outputs rather than depend on internal resource addresses. Encapsulation allows internal refactoring, but changes to output names, types, or meaning can break users and need a migration or versioned release. For example, a module returns a map of subnet IDs keyed by zone; changing it to a list without notice can break callers.

WORKED EXAMPLE
A module returns a map of subnet IDs keyed by zone; changing it to a list without notice can break callers.

A caller consumes module.network.private_subnet_ids as a map keyed by zone. An internal refactor can change resource names without changing that output. However, changing the output into an ordered list alters the caller's contract even if the same subnet IDs appear. Downstream selection may become incorrect or fail validation.

Encapsulation hides implementation behind a defined interface. An output exposes selected results. A breaking change invalidates an existing caller assumption. Preserve names, types, and semantic meaning where possible, or version and document migration. Avoid outputting whole sensitive resource objects simply to save time. Stable interfaces reduce coupling, but cannot eliminate every external dependency: callers still rely on operational properties such as routing and allowed traffic that an ID alone does not describe. Document those properties. Internal moved blocks can preserve state relationships during refactoring, while output compatibility preserves configuration relationships between modules.

Why this matters

A module returns a map of subnet IDs keyed by zone; changing it to a list without notice can break callers.

A common trap

Do not overlook this operational distinction: Encapsulation allows internal refactoring, but changes to output names, types, or meaning can break users and need a migration or versioned release.

Versioning and examples

Version reusable modules and pin callers to deliberate releases or refs. Examples should document prerequisites, expected plans, costs, and cleanup. Module tests should exercise real interface assumptions rather than mirror every line. Avoid claiming a reusable module is safe for all organizations without stated scope. For example, a production caller pins a reviewed module release instead of tracking a mutable main branch unexpectedly.

WORKED EXAMPLE
A production caller pins a reviewed module release instead of tracking a mutable main branch unexpectedly.

A production caller tracks a module's main branch. The author changes a default, and the next initialization pulls behavior the production team never reviewed. Pinning a release or immutable ref gives the caller control over when that change enters its plan. Upgrading then becomes an explicit comparison and test rather than an incidental download.

A version identifies a published module release. A compatibility promise describes which interface changes are expected. An example is a runnable usage scenario with stated prerequisites and scope. Provider locking and module version selection are separate mechanisms; the provider lock file does not freeze all module source choices. Maintain examples and test important contracts, including validation and output shapes. Pinning old versions improves reproducibility but does not justify ignoring security fixes indefinitely. Review release notes, update deliberately, and inspect the plan against a representative caller before promoting the version to critical environments.

Why this matters

A production caller pins a reviewed module release instead of tracking a mutable main branch unexpectedly.

A common trap

Do not overlook this operational distinction: Avoid claiming a reusable module is safe for all organizations without stated scope.

KEEP THESE WITH YOU

Today’s takeaways

Inputs, defaults, and validation: A module interface should expose choices callers genuinely need while retaining safe defaults. Validate meaningful constraints and explain units. Too many unrelated toggles make behavior difficult to reason about; too few inputs force callers to copy and modify the module.

Outputs and encapsulation: Outputs provide selected module results. Callers should use outputs rather than depend on internal resource addresses. Encapsulation allows internal refactoring, but changes to output names, types, or meaning can break users and need a migration or versioned release.

Versioning and examples: Version reusable modules and pin callers to deliberate releases or refs. Examples should document prerequisites, expected plans, costs, and cleanup. Module tests should exercise real interface assumptions rather than mirror every line. Avoid claiming a reusable module is safe for all organizations without stated scope.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed