All posts
contractsterraformplatform-engineering

Beyond Terraform Modules: Infrastructure Design by Contract

Terraform modules wired by string-matching fail late and cost every team the same work again. Typed module contracts move that work to the module author, once, and check every change before it runs, whether a person or an AI agent makes it.

Anshul Sao
Anshul Sao
May 12, 2025 · 11 min read

It's 2AM. You're staring at a failed deployment pipeline, trying to figure out why your EKS cluster can't find the right subnets. You've checked the variables, verified the outputs, and triple-checked your depends_on statements. Everything looks correct, yet it still fails.

This scenario plays out daily across organizations that have adopted Terraform. Modules promised composable, reusable infrastructure. In practice, they became a web of string-matching, implicit dependencies and tribal knowledge.

The Hidden Tax of Module Stitching

Every time a team provisions infrastructure for a new service or application, it pays an invisible tax. The tax has three parts.

Hunt & Wire

For each input a module needs, someone must make a decision: Is this value a hard-coded constant? A data lookup? An output from another module? The answer differs for every project and environment, so the decision tree is rebuilt from scratch each time.

Take a simple task: connect a database to a VPC. First, you read the VPC module to find its outputs:

hcl
1234567
# From terraform-aws-modules/vpc/aws
output "vpc_id" {
value = aws_vpc.this.id
}
output "private_subnets" {
value = aws_subnet.private[*].id
}

Then you read the database module to find what it expects:

hcl
1234567
# From terraform-aws-modules/rds/aws
variable "vpc_security_group_ids" {
type = list(string)
}
variable "subnet_ids" {
type = list(string)
}

Then you write the wiring you hope is correct:

hcl
123456
module "db" {
source = "terraform-aws-modules/rds/aws"
subnet_ids = module.vpc.private_subnets
vpc_security_group_ids = [aws_security_group.database.id]
# Database configuration...
}

terraform plan accepts it, because every value is a list of strings. The error comes at apply:

text
123
Error: Error creating DB Instance:
InvalidParameterCombination:
RDS DB Subnet Group doesn't match VPC

Back to the documentation. Fix and try again. Nothing checked the wiring until the cloud API did, long after the pull request was merged.

Re-implement Environment Rules

Resource naming, tagging strategies, IAM permissions and security controls must be applied the same way in every environment. Yet traditional modules handle environments inconsistently: sometimes a module accepts an environment parameter, sometimes it doesn't, and nothing enforces correct usage.

The burden of "getting production right" sits on whoever writes the module composition code, with no systematic guardrails.

Maintain a Second Codebase

The glue code that ties modules together grows into a project-sized Terraform configuration:

hcl
1234567
# Just a small sample of typical glue code
locals {
vpc_name = "${var.prefix}-${var.environment}-vpc"
private_subnets = module.vpc.private_subnet_ids
cluster_name = "${local.vpc_name}-eks"
# ... dozens more variable mappings
}

This "composition layer" often holds more logic than the modules underneath it.

Module variation makes it worse. Two VPC modules that do the same job rarely name their outputs the same way:

hcl
1234567
# VPC module A
output "vpc_id" { value = aws_vpc.this.id }
output "subnet_ids" { value = aws_subnet.private[*].id }
 
# VPC module B
output "id" { value = aws_vpc.main.id }
output "private_subnet_ids" { value = aws_subnet.private[*].id }

Every variation adds more reading and more debugging, and modules from different sources add more still.

The result is a silent cost curve. Each new service adds the same wiring work as the last one. At scale, that work caps delivery speed, security posture and team morale.

Four Pillars of Infrastructure Design by Contract

Software engineers have long used Design by Contract to build reliable, modular systems. Each component makes a formal agreement about what it requires and what it provides. Applied to infrastructure, the idea rests on four pillars.

1. Contract-Led Composition: Explicit Interfaces Between Components

Traditional reality: Integration is a string-matching game. You hope the VPC module's outputs match what the EKS module expects, and nothing confirms it until runtime:

hcl
12345678910111213141516
# Traditional Terraform approach
# VPC module outputs
output "vpc_id" { value = aws_vpc.this.id }
output "subnet_ids" { value = aws_subnet.private[*].id }
 
# EKS module variables
variable "vpc_id" { type = string }
variable "subnet_ids" { type = list(string) }
 
# Manual wiring in the root module
module "eks" {
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.subnet_ids
# No check that these are the correct subnets
# No guarantee vpc_id is from a compatible VPC
}

With a contract: Each module declares what it gives and what it needs, as named types. In Facets, a module is Terraform plus a facets.yaml file, and the relevant parts look like this:

yaml
12345678910111213
# VPC module, facets.yaml (trimmed): what it gives other modules
outputs:
default:
type: '@facets/aws-vpc-details'
 
# Kubernetes cluster module, facets.yaml (trimmed): what it needs
inputs:
cloud_account:
type: '@facets/aws_cloud_account'
providers:
- aws
network:
type: '@facets/aws-vpc-details'

Diagram that compares string wiring with a typed contract. On the left, string wiring: a VPC module gives outputs such as vpc_id and private_subnets, someone guesses which ones the Kubernetes module needs, and the mismatch shows up only at apply time, so every team wires it again. On the right, a typed contract: the VPC module gives an output of type @facets/aws-vpc-details and the Kubernetes module needs an input of the same type. The types match, so the wiring is checked before plan, and the module author defines it once.

The type is the contract. Facets wires an input to an output only when the type names match, so any module that gives @facets/aws-vpc-details, a VPC module or a landing-zone module, can satisfy the cluster's network input. Before anything is provisioned, Facets checks that:

  • every input is wired to an output of the type it expects,
  • the settings a person or an agent chose match the module's spec, which is a JSON Schema,
  • the wiring follows the dependencies, so resources are created in the right order.

So a wiring mistake shows up before plan, and the deploy, fail, debug and redeploy cycle goes away. Modules explains how types are named, registered and versioned.

2. Environment-Aware by Design: Contracts Across Environments

Traditional reality: Environment handling is bolted on afterwards. Teams create folder structures per environment, copy config files, or stack variable overrides that drift out of sync.

With a contract: The module states which fields may change per environment and which may not:

yaml
123456789
vpc_cidr:
type: string
default: "10.0.0.0/16"
x-ui-overrides-only: true # set per environment only
 
enable_dns_hostnames:
type: boolean
default: true
x-ui-override-disable: true # set in the blueprint, never per environment

The contract now covers environments too. A module author does not hope that every caller handles production correctly. The author decides, in the module, what an environment may change. Form UI (x-ui tags) lists these fields.

3. Safe Upgrades: Versioned Contracts

Traditional reality: Infrastructure updates are all-or-nothing. "Merge this change to every environment and hope nothing breaks" is the unstated strategy.

With a contract: A module carries a major version in its facets.yaml. A breaking change ships as a new major version beside the old one, so projects on the old version keep working. A new module starts in preview, where only test projects can use it, and a change is tested in a test project before it is published. When a new major version is published, new resources use it by default, existing resources get a notice that it is available, and each project moves to it when it is ready.

So a platform team can roll out support for a new Kubernetes version without a big-bang upgrade. Versioning strategy gives the rules.

4. Developer Experience That Preserves Governance: Preconditions

Traditional reality: Platform teams face a hard choice: lock everything down and become a bottleneck, or expose raw Terraform and risk security and compliance gaps. The common answer is an approval workflow that slows everyone down.

With a contract: The settings a developer may change are a schema, with allowed values and limits. A field can also appear only when another field makes it relevant:

yaml
12345678910
backup_mode:
type: string
enum: ["none", "daily", "point_in_time"]
retention_days:
type: integer
minimum: 1
maximum: 35
x-ui-visible-if:
field: spec.backup_mode
values: ["daily", "point_in_time"]

These are preconditions in the Design by Contract sense. A value outside the enum or the limits does not pass, and a field shows up only when it applies. The form stays small for the developer, and the governance boundary stays where the platform team put it.

From Configuration to Contract-Driven Infrastructure

Moving from plain modules to modules with contracts changes how a team works. Design by Contract gives infrastructure:

  • explicit guarantees about how components connect,
  • checkable limits on what can change between environments,
  • clear boundaries between the teams that write modules and the teams that use them,
  • the same interfaces across the whole organization.

It also gives a cleaner split of the work:

  • Platform engineers build modules with explicit contracts.
  • Developers state what they need through checked interfaces.
  • The orchestrator checks every change and enforces the contracts.

A team that adopts contracts keeps Terraform. The contract is a framework on top of it, in the way Spring Boot sits on top of Java: it adds structure, conventions and higher-level building blocks, and the full power of the language stays underneath.

Why contracts matter more when agents write infrastructure

You cannot vibe code infrastructure.

An AI agent learns in a loop: it acts, it checks the result, and it corrects itself. In code, the compiler and the tests do the check, in seconds, and nothing breaks. In infrastructure, the usual check is the cloud API at apply time, as the RDS error above shows. By then, the mistake has already reached a real environment.

AI assistants now write Terraform too, so the number of changes grows while the wiring stays manual. A contract moves the check to the front. The agent fills in the allowed settings and declares what connects to what, and the contract rejects a wrong type or an out-of-range value before plan. If a change does not comply, it does not deploy.

Agents can use the building blocks, but they cannot change the rules inside them. Encryption, network placement and backups stay where the platform team put them. Why Facets explains why this check is what lets an agent work on infrastructure at all.

Design by contract in Facets

How Facets does it

Facets is the infrastructure delivery platform for developers and their AI assistants, and it is built on these principles. Each module is Terraform plus a facets.yaml contract. The platform team writes a module once, and every project builds from it.

Developers fill in a form that Facets builds from the contract, or they ask their AI assistant, such as Claude Code, Codex or Cursor, through the Praxis CLI. Facets wires the outputs of one module to the inputs of the next in each environment, so nobody copies a VPC ID or a database host by hand. Praxis can also write a new module and test it in a test environment before it is published (Build through Praxis).

A Fortune-50 automotive company reduced DevOps tickets by 75% across 350 applications and 1,500 engineers.

Diagram of what a contract holds, with a Postgres module as the example. The developer chooses a few settings: size, version and storage. The module needs a network and a cloud account, and it gives connection details to the services that use it. The platform team decides the rest inside the module, such as encryption, network placement, backups and monitoring. The contract is typed, so a wrong value cannot pass. It is versioned, so a new version is tested before release. It is repeatable, so it gives the same result in every environment.

Contracts also change who pays the integration tax:

Plain Terraform modulesModules with contracts
Who paysEvery team that uses a moduleThe module author and the platform team
WhenEvery time a team provisions infrastructureOnce, when the module is written or changed
How oftenFor every project and every environmentFor each new module version

Module use happens far more often than module creation, so the total cost falls as more teams build from the same modules.

Getting started

You don't need to change everything at once:

  1. Measure the integration work. Track the time your teams spend wiring modules and fixing wiring errors.
  2. Find the high-friction modules. Start with the ones that every project wires by hand, such as networks, clusters and databases.
  3. Wrap existing modules in contracts. Add a facets.yaml and keep your Terraform as it is (Using your existing Terraform modules).
  4. Let the check run on every change. Once a module has a contract, every use of it is checked before plan, whoever makes the change.

You can also browse Facets' open-source Terraform modules on GitHub.

Contracts are the first of three things that close the loop for AI-era delivery. The next step is declaration: one model of the whole system, built from these modules, explained in What is an infrastructure blueprint?. Infrastructure orchestration shows how the parts fit together, and Why Terraform automation tools fall short compares Facets with other Terraform platforms.

Book a demo to see contract checks on your own modules.

Frequently asked questions

Tags

#terraform-enterprise#terraform#design-by-contract