Terraform Academy

Research study TA-R001 · Revision 1

Testing Terraform module contracts without cloud credentials

Terraform Academy Research · Evidence reviewed

A reproducible Terraform experiment testing naming rules, tag contracts and expected input validation failures without provisioning infrastructure.

Research question

Can Terraform's native plan-based tests verify deterministic module output contracts and reject invalid inputs without connecting to a cloud provider?

Recorded result

4 tests passed, 0 failed. Terraform 1.12.2; historical CI execution on 2026-11-30. Original workflow run.

Methodology

The configuration defines two string inputs, environment and service. Environment accepts dev, test or prod. Service names must match a lowercase naming pattern, starting with a letter and containing 2 to 24 characters.

The module combines inputs into a name and returns three tags. Each test uses command = plan, so the tests evaluate the declared behavior without an apply operation.

The development case checks both the generated name and the complete tag map. The production case checks a different valid name. Two negative cases use expect_failures to specify the variable validation that must reject the invalid input.

Observed results

Each row represents a native test run, not a count of individual assertions.

development name and tags

Input: dev / catalog
Contract: dev-catalog and the complete expected tag map
Observed: PASS

production name

Input: prod / api
Contract: prod-api
Observed: PASS

reject unknown environment

Input: unknown / catalog
Contract: Environment variable validation rejects input
Observed: PASS

reject invalid service

Input: test / Bad Name
Contract: Service variable validation rejects input
Observed: PASS

Success! 4 passed, 0 failed.

Formatting, initialization and validation also completed successfully in the recorded job.

Reproduce the experiment

Use the pinned source revision below, with Terraform 1.12.2 installed. Start from a clean working directory. Review all configuration and test files before running them. The example contains no cloud resources and needs no cloud credentials; installing Terraform itself may require network access.

Pinned source files · Recorded job

git clone https://github.com/everybitcounts/terraform-academy-oss.git
cd terraform-academy-oss
git checkout 860b04dbb6f204c3bf3d89387e65893aee932564
cd examples/module-contracts
terraform version
terraform fmt -check -recursive
terraform init -backend=false -input=false
terraform validate
terraform test -no-color

Recorded CI checkout revision: 99e2be596bc0b3fd0e59d01fb84efe0c488184e8. Experiment file blobs were checked against the pinned source and CI checkout. Runner: ubuntu-26.04.

Engineering interpretation

For this specific configuration, the recorded run supports the hypothesis: Terraform evaluated output expressions and enforced the tested input constraints without requiring cloud resources. Format and validation succeeded before the test suite executed.

This distinction is useful in CI. Formatting and static validation are separate gates from behavioral tests. A naming expression can be syntactically valid yet still violate the contract consumed by downstream automation.

The negative cases are as important as the positive ones. A module interface should define what it accepts and how it rejects inputs. An expected validation failure that occurs as intended is a passing test.

A practical next experiment is to remove the environment prefix in a disposable copy, run validation and then execute the test suite. The hypothesis is that the modified expression remains valid HCL while the naming assertions fail. That mutation experiment is proposed, not part of the recorded four-test result.

Limitations

Practical takeaway

Use contract tests to verify deterministic naming, tags and input validation before advancing a module to provider-specific integration tests. A passing local test is one layer of evidence, not a production readiness certificate.

Continue learning

Primary references

Return to Terraform Academy Research