Go to content
Infrastructure

Testing Terraform Modules Before Release

Terraform modules are reusable packages of infrastructure configuration. Other repositories pull a module by version and supply input variables; the module creates resources such as servers, databases, and networking, and returns output values those consumers read. When you publish a new module version, every consumer that updates to it inherits your change. A renamed variable, a dropped output, or a resource that now forces replacement becomes their problem, with no warning until their next plan or apply.

Releasing a new module without testing can surface unexpected issues in production and cause downtime, so versioning best practices put automated checks with validate, plan, and apply at the baseline for every release. It ends with a lightweight workflow a small team can run without a dedicated infrastructure platform team.

A few terms before the mechanics. terraform validate parses a configuration and checks that it is internally consistent, meaning syntax, resource types, required arguments, and references line up with the provider schema. It has no access to state or deployed infrastructure. terraform plan reads the state file, refreshes it against real infrastructure, and diffs the configuration against that refreshed state. terraform apply makes the planned changes real. The sections below show when each tool catches a real regression and where it misses.

Interface regressions and contract tests

An interface regression is a change that breaks the contract consumers rely on. The contract has three parts: the input variables a consumer sets, the output values a consumer reads, and the resources the module creates. A regression can be a renamed variable, a variable whose type changed from string to list, an output removed from the output block, or a provider upgrade that changes how an argument is interpreted.

Contract tests guard this layer. They ensure the contract between a configuration’s expected inputs and the module’s actual inputs has not been broken, which makes them a special kind of unit test for Terraform. You write a fixture that declares inputs the way a real consumer would and asserts the outputs that consumer reads, then run it against the module as it changes.

Resource behavior is the third part of the contract. A change that forces replacement shows up in terraform plan as a -/+ line with a # forces replacement annotation, and that line is the only signal in the toolchain that an existing resource will be destroyed and recreated. Consumers see it when they run plan on your new version. You want to see it before you tag that version. For a deeper look at keeping these interfaces stable across environments, see designing stable Terraform module interfaces.

The cheap checks: validate and fmt

terraform validate is the cheapest check you can run, and it belongs in every pull request. It parses the configuration and verifies internal consistency: syntax, resource types, required arguments, and references all line up against the provider schema. Because it touches neither state nor deployed infrastructure, it runs in seconds and needs no credentials.

terraform fmt -check complements it by flagging formatting drift, which keeps diffs readable. Together the two serve as a first-pass unit check for a configuration.

The limit matters: a clean validate result does not mean a change is safe. Validate cannot detect forced replacements, so it will not tell you that a provider upgrade will destroy and recreate a resource. That class of problem only appears in plan or apply. Treat a clean validate as necessary but not sufficient.

Plan reviews in CI

The most valuable CI check for a module is running terraform plan against real example configurations, because plan is the only tool that shows the concrete diff a consumer will get. A common pattern is a pull request workflow that runs terraform fmt -check, tflint, terraform init, terraform validate, and terraform plan, then posts the plan output back to the pull request comments using actions/github-script. The plan text stays in the PR thread, so reviewers see the exact resource changes without running anything locally.

Two workflow details matter. Add a concurrency rule so the workflow does not launch multiple runs of the same PR or branch simultaneously, which can produce interleaved plans. Use -no-color on the plan command so the output in the PR comment contains no ANSI escape characters.

The plan review has a known weakness: the plan is a snapshot. Between the moment the workflow computes it and the moment the PR merges, infrastructure or shared state can change, so the merged plan may not match what was reviewed. Treat the PR plan as a review aid, and re-run plan after merge in the release pipeline rather than trusting the PR output.

Example configurations are the input to these plans. A module should ship with one or more example directories, each a complete root configuration that consumes the module with realistic inputs. Running plan against each example exercises the module the way consumers will.

Apply and destroy tests for real confidence

Plan tells you what will change; apply tells you whether it works. The strongest pre-release test runs a full cycle in a disposable environment: terraform init, terraform plan, terraform apply, assert the results, then terraform destroy. A module delivery pipeline can follow this shape: plan first, run unit tests including contract tests, then apply, run integration tests, remove the resources, and only then release a new module version.

The assertions matter more than the apply itself. Checking that a resource exists is weak; checking that it works is strong. For an Azure Function App, a meaningful test validates that the application code is deployed, the triggers fire, and the endpoint responds correctly. You can build those checks with Terraform’s built-in http data source, which asserts on status_code and response_body, and the local_sensitive_file data source can verify the deployment package exists and is readable before you provision anything.

For Go-based teams, Terratest is the established framework: you call terraform.InitAndApply and then assert on the state of resources and output variables. The Caylent module hub requires all modules to be fully tested with rego and Terratest, semantically versioned, and released through CI/CD, and it runs a weekly health check that exercises every module. If you depend on third-party providers, the same discipline as evaluating open source dependency risk applies to the provider releases your module consumes.

Applying real resources costs money and time. Keep the test resources small, run integration tests less frequently than unit tests, set aggressive timeouts, and tag resources with a TTL so cleanup can find them. Prefer plan-only tests where they give enough signal. For drift detection, terraform plan -detailed-exitcode returns exit code 0 for no changes, 1 for an error, and 2 when changes exist, which a scheduled job can use to flag infrastructure that drifted from the module.

A lightweight workflow for small teams

A small team can get strong coverage with three workflows and no dedicated platform tooling.

The pull request workflow is the gate. On every PR, run terraform fmt -check, terraform init, terraform validate, and terraform plan against each example configuration, and post the plan to the PR. This catches syntax errors, contract breakage, and forced replacements before a reviewer spends time on the diff, and it needs no cloud credentials.

The release workflow runs on merge to main or on a version tag. It provisions a small sandbox environment, applies each example, runs the integration assertions, destroys everything, and only then publishes the module version. Store credentials in GitHub Secrets and isolate the test environment from anything real.

The weekly check is the safety net. Schedule a job that runs terraform plan -detailed-exitcode against a long-lived test environment. Exit code 2 means the module’s plan drifted from what the environment actually has, which points at a provider update, a manual change, or a bug in the module. Most teams scale from nightly regression tests and drift detection toward provider compatibility and smoke tests as needs grow. For guidance on organizing the repository around modules and environments, see structuring Terraform repositories.

More in Infrastructure Back to top ↑