liveBook · Manning

Contract

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A defined software interface through which one system invokes operations or accesses capabilities of another system.

A contract is the part of an interface that other people or programs rely on. In Terraform work, a module’s variable names, a command’s JSON output, and a provider’s available resources can all become contracts. Once others depend on them, even a small change can affect their work. section 9.6 section 11.1.1 section 12.1

A small name, a recurring cost

Suppose two modules both accept subnet IDs, but one calls its input `subnets` and the other calls it `subnet_ids`. Both may work, yet users must keep checking which name each module expects. Standardizing an interface can improve usability; changing an existing name also requires care because users may already depend on it. section 9.6

Identify the contract before building on it:

  • For a module, decide which inputs users will supply and keep their names consistent across related modules. section 9.6
  • For a command wrapper, use `-json` rather than parsing human-readable output. Single-response commands such as `show` return one JSON object; commands such as `plan` stream independently parseable JSONL records. section 11.1 section 11.1.1
  • For a provider, decide which resources, data sources, and functions users should have before writing code. A backward-incompatible provider change may affect many modules. section 12.1
Listing 12.1 Mastodon provider examples
terraform {
  required_providers {
    mastodon = {
      source = "terraformindepth/mastodon"
    }
  }
}
 
provider "mastodon" {                  #1
  host = "https://hachyderm.io"
}
 
data "mastodon_account" "me" {         #2
  username = "tedivm"
}
 
resource "mastodon_post" "this" {      #3
  content = "Hello, World!"
}

The provider example shows the intended user-facing surface: host configuration, an account lookup, and a post resource. Designing those uses first makes the interface easier to assess before implementation. section 12.1

Treat machine-readable formats as documented contracts, not as incidental display text. A wrapper must run commands, handle errors, parse their results, and check current field documentation for changes. Likewise, settle provider interfaces with compatibility in mind before other modules begin relying on them. section 11.1 section 11.1.1 section 12.1

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains interface design through variable naming and provider planning, then distinguishes single-response JSON from streaming JSONL without specifying every output field.
Examples
Uses mismatched subnet variable names, `show` and `plan` output, and a provider configuration with an account lookup and post resource.
What it assumes
Assumes familiarity with Terraform commands for wrapper work; requires Go knowledge to maintain a provider but not Python knowledge to follow the wrapper example.
What you can do after
Build it from scratch by defining provider resources, data sources, and functions around intended user actions before implementation.
How it connects
Connects contracts to Terraform modules, provider resources, data sources, functions, command wrappers, and backward compatibility.

Also explained in

Compare how 44 books explain contract →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.