TExcelTAD
Home › Platform Engineering: Internal Developer Platforms › Terraform Modules as a Catalog

Terraform Modules as a Catalog

The Terraform course taught how to write a module. A platform team writes a catalog of them: a small set of opinionated, versioned modules (a PostgreSQL database, a bucket, a queue) that bake in the company's standards (encryption, backups, tags, alarms), so a team asking for a database writes ten lines instead of two hundred and gets a compliant one every time.

What makes a module a platform product

Property Why
Small interface inputs are decisions a team should make (size, name, owner), not every provider argument. size = "m" maps to an instance class inside the module
Secure and compliant by construction encryption, backups, deletion protection, tags and alarms are not optional inputs
Versioned with semver teams pin a version and upgrade deliberately; a breaking change is a major version with upgrade notes
Documented and discoverable a README generated with terraform-docs, and an entry in the portal
Tested terraform test with mocked providers (Terraform: testing), plus a real apply in a sandbox account on release
Owned a team on the hook for bugs and upgrades, named in the catalog

A consumer's whole database, using the catalog's module:

hcl
module "shortlinks_db" {
  source = "git::https://github.com/acme/terraform-modules.git//modules/postgres?ref=postgres/v2.0.0"

  name  = "shortlinks"
  owner = "team-growth"
  size  = "s"          # s | m | l: instance class, storage and IOPS are decided by the module
  tier  = "standard"   # critical adds Multi-AZ and longer backup retention
}

A monorepo of modules with per-module tags

Two common layouts: one repository per module (terraform-aws-postgres, the convention the public registry requires) or one monorepo with a tag per module and version (postgres/v2.0.0). The monorepo is easier to maintain for a small platform team; the //modules/postgres part of the source address selects the subdirectory and ?ref= the tag (Terraform: module sources).

The session builds such a repository, then produces the catalog index a portal page or a CLI would show: one entry per module, its latest version, owner and the exact source line to copy:

bash · try it yourself
git init -q terraform-modules && cd terraform-modules
add_module() {   # name version description
  mkdir -p "modules/$1"
  printf 'name: %s\nowner: team-platform\ndescription: %s\n' "$1" "$3" > "modules/$1/module.yaml"
  printf '# %s %s\n' "$1" "$2" > "modules/$1/main.tf"
  git add . && git commit -qm "$1 $2" && git tag "$1/v$2"
}
add_module postgres 1.0.0 "PostgreSQL on RDS with backups, encryption and alarms"
add_module bucket   1.0.0 "S3 bucket with encryption, versioning and no public access"
add_module postgres 1.1.0 "PostgreSQL on RDS with backups, encryption and alarms"
add_module postgres 2.0.0 "PostgreSQL on RDS with backups, encryption and alarms"
add_module bucket   1.1.0 "S3 bucket with encryption, versioning and no public access"
add_module queue    0.3.0 "SQS queue with a dead-letter queue"
cd ..
echo "tags:"
git -C terraform-modules tag -l | sort -V | paste -sd " "
echo "catalog:"
for m in $(git -C terraform-modules tag -l | cut -d/ -f1 | sort -u); do
  latest=$(git -C terraform-modules tag -l "$m/v*" | sort -V | tail -1)
  git -C terraform-modules show "$latest:modules/$m/module.yaml" |
    yq -o=json ". + {\"latest\": \"${latest#*/}\", \"source\": \"git::https://github.com/acme/terraform-modules.git//modules/$m?ref=$latest\"}"
done | jq -c "{name, latest, owner, source}"
tags:
bucket/v1.0.0 bucket/v1.1.0 postgres/v1.0.0 postgres/v1.1.0 postgres/v2.0.0 queue/v0.3.0
catalog:
{"name":"bucket","latest":"v1.1.0","owner":"team-platform","source":"git::https://github.com/acme/terraform-modules.git//modules/bucket?ref=bucket/v1.1.0"}
{"name":"postgres","latest":"v2.0.0","owner":"team-platform","source":"git::https://github.com/acme/terraform-modules.git//modules/postgres?ref=postgres/v2.0.0"}
{"name":"queue","latest":"v0.3.0","owner":"team-platform","source":"git::https://github.com/acme/terraform-modules.git//modules/queue?ref=queue/v0.3.0"}

The index is generated from Git, so it can't drift from what's released. A CI job on every tag can publish it as JSON for the portal, or register each module in Backstage as a catalog entity (a Resource type such as terraform-module, or a software template that writes the module block into the team's infrastructure repository).

If you use a private registry instead (HCP Terraform / Terraform Enterprise, or a registry-protocol server for OpenTofu), the source becomes app.terraform.io/acme/postgres/aws with a version constraint, and the registry provides search and docs.

Version constraints, modelled

With a registry source, consumers write constraints such as version = "~> 1.1". Terraform picks the newest released version that satisfies all of them. A model of the common operators (=, >=, <, ~>), showing why ~> is the recommended way to accept fixes but not breaking changes:

python · try it yourself
RELEASES = ["0.9.0", "1.0.0", "1.0.3", "1.1.0", "1.2.0", "2.0.0", "2.1.0-beta.1"]

def parse(v):
    core = v.split("-")[0]
    return tuple(int(x) for x in core.split(".")), "-" in v

def satisfies(version, constraint):
    (ver, pre) = parse(version)
    for part in constraint.split(","):
        op, want = part.split()
        w = tuple(int(x) for x in want.split("."))
        if op == "~>":            # allow the rightmost given component to increase
            upper = list(w[:-1]); upper[-1] += 1
            ok = ver >= w + (0,) * (3 - len(w)) and ver < tuple(upper) + (0,) * (3 - len(upper))
        else:
            w3 = w + (0,) * (3 - len(w))
            ok = {"=": ver == w3, ">=": ver >= w3, "<": ver < w3}[op]
        if not ok:
            return False
    return not pre               # this model never selects pre-releases (Terraform needs an exact version for them)

for c in ["~> 1.0", "~> 1.0.0", ">= 1.0, < 2.0", "= 2.0.0", ">= 1.2"]:
    ok = [v for v in RELEASES if satisfies(v, c)]
    print(f"{c:15} -> {', '.join(ok):32} picks {ok[-1]}")
~> 1.0          -> 1.0.0, 1.0.3, 1.1.0, 1.2.0       picks 1.2.0
~> 1.0.0        -> 1.0.0, 1.0.3                     picks 1.0.3
>= 1.0, < 2.0   -> 1.0.0, 1.0.3, 1.1.0, 1.2.0       picks 1.2.0
= 2.0.0         -> 2.0.0                            picks 2.0.0
>= 1.2          -> 1.2.0, 2.0.0                     picks 2.0.0

~> 1.0 accepts any 1.x, ~> 1.0.0 only patch releases of 1.0. The pre-release 2.1.0-beta.1 is never picked by a range constraint. Module Git sources have no constraints: ?ref= pins one exact tag, and Renovate or Dependabot opens the upgrade pull requests.

Rolling out a new major version

When postgres goes from 1.x to 2.0 (say, a new default engine version that needs a maintenance window):

  1. Release 2.0.0 with an upgrade guide; keep fixing 1.x for an announced period.
  2. Find consumers: search the infrastructure repositories for modules/postgres?ref=postgres/v1, or read the catalog if modules are registered there.
  3. Open upgrade pull requests (Renovate can do it), with a moved block or state commands when resources are renamed (Terraform: moved).
  4. Track how many consumers are left on 1.x, exactly like template versions (Keeping Services in Step).

Test yourself

Which input belongs in a platform module's interface?

  1. storage_encrypted (true/false)
  2. size (s/m/l)
  3. backup_retention_period (any number)
  4. kms_key_policy (raw JSON)

Which versions does ~> 1.0 allow?

  1. Only 1.0.x
  2. Any 1.x at or above 1.0, but not 2.0
  3. Any version >= 1.0
  4. Only 1.0.0

In git::https://github.com/acme/terraform-modules.git//modules/postgres?ref=postgres/v2.0.0, what does //modules/postgres do?

  1. It is a comment
  2. It selects a subdirectory of the repository as the module
  3. It names the tag
  4. It sets the provider