Terratest 2.0: Modular Architecture and Terraform Testing Without Bloated SDKsArticle · digest for October 11
Modular split of the Terratest 2.0 framework into a compact core and independent cloud packages.
Article4 sections≈ 5 min

Terratest 2.0: Modular Architecture and Terraform Testing Without Bloated SDKs

Every Go engineer who has written automated cloud infrastructure tests with Terratest knows that strange moment. You want to test a single simple Terraform manifest that spins up a virtual machine, yet go get starts downloading half the cloud internet. Huge SDKs from AWS, Azure, Google Cloud, and OCI get pulled into your project, along with heavyweight Kubernetes client libraries.

As a result, a compact infrastructure repo swells with a go.sum file hundreds of lines long. CI runners waste most of their time downloading and compiling transitive dependencies that your test never even touches. The long-awaited major release of Terratest 2.0 from Gruntwork finally solves this architectural pain.

The "heavy suitcase" problem: why go.sum kept growing

Terratest v1 was designed as a Swiss Army knife: a single library covering everything from building Packer images and deploying Helm charts to running Terraform commands and SSH checks. That versatility came at the cost of a monolithic Go module structure.

If your own codebase used a newer version of AWS SDK v2, the old monolithic Terratest could clash with it and trigger painful dependency hell. A basic test required pinning 675 lines of dependencies in go.sum. Developers joked it was like traveling light while dragging someone else's giant suitcase.

Splitting into 16 modules and moving the core to core/v2

In version 2.0, the monolith was split into 16 focused modules. Each one is versioned independently and uses a /v2 suffix in its import path.

Key architectural changes include:

  • Extracting the core into modules/core/v2: general-purpose helpers—random string generators (random), retry mechanisms (retry), system loggers (logger), and file/shell utilities—now live in a light, standalone core with zero vendor lock-in.
  • Shrinking the dependency graph: for a project that only validates HCL code via modules/terraform/v2, go.sum dropped from 675 lines to 77—nearly a tenfold reduction.
  • Unified package names: hyphens were removed from paths to follow Go best practices. Packages like http-helper, dns-helper, and test-structure became httphelper, dnshelper, and teststructure.
  • Dropping legacy dead weight: unused and unmaintained modules (collections, environment, git, version-checker, slack, and oci) were removed completely.

Migration in practice: updating go.mod and writing a Terraform test

Upgrading to the new version requires explicit updates in go.mod. You now declare only the cloud providers your scenarios actually use:

module my-infra-tests

go 1.23.0

require (
    github.com/gruntwork-io/terratest/modules/core/v2 v2.0.0
    github.com/gruntwork-io/terratest/modules/terraform/v2 v2.0.0
    github.com/stretchr/testify v1.9.0
)

Import paths in your test files change as well. Instead of reaching deep into the old monolith, you import the core utilities and the target Terraform runner directly:

package test

import (
    "testing"

    "github.com/gruntwork-io/terratest/modules/core/v2/logger"
    "github.com/gruntwork-io/terratest/modules/core/v2/random"
    "github.com/gruntwork-io/terratest/modules/terraform/v2"
    "github.com/stretchr/testify/assert"
)

func TestWebServerDeployment(t *testing.T) {
    t.Parallel()

    // Generate a unique prefix to isolate cloud resources
    uniqueID := random.UniqueId()
    instanceName := "web-node-" + uniqueID

    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples/web-cluster",
        Vars: map[string]interface{}{
            "instance_name": instanceName,
        },
        Logger: logger.Default,
    })

    // Guarantee cleanup of created resources even if the test fails
    defer terraform.Destroy(t, terraformOptions)

    terraform.InitAndApply(t, terraformOptions)

    endpoint := terraform.Output(t, terraformOptions, "service_endpoint")
    assert.NotEmpty(t, endpoint)
}

Without extra SDKs in the way, test compilation starts immediately and CI module caches stay free of megabytes of unused binaries.

Breaking changes and the ExternalIP gotcha

Because Terratest 2.0 is a major release with breaking changes, you can't upgrade automatically without touching code. Teams will need to batch-refactor import paths across all test files. The v1 branch will stay on life support for 12 months, receiving only critical security patches.

Infrastructure engineers should also watch for a subtle change in the Kubernetes helpers. When fetching cluster node addresses, the logic now prefers the public ExternalIP over the private InternalIP. If your tests run inside an isolated corporate VPC with no direct outbound access, you may need to override connection settings explicitly.

For all new projects and active monorepos, moving to Terratest 2.0 is an easy recommendation that brings speed and predictability back to your builds.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2