Terratest in CI/CD
Credentials, cleanup, gating.
Running Terratest in CI gives you the payoff — every module change is proven to actually work before merge — but it needs care because the CI job now deploys real infrastructure. The essentials: give the job short-lived, sandbox-scoped cloud credentials via OIDC (never long-lived keys, never production access), set generous timeouts, ensure cleanup runs even on failure or cancellation, and gate it appropriately (module PRs run the suite; not every unrelated commit needs it).
jobs:terratest:permissions: { id-token: write, contents: read } # OIDCsteps:- uses: aws-actions/configure-aws-credentials@v4with: { role-to-assume: arn:aws:iam::SANDBOX:role/terratest, aws-region: us-east-1 }- run: cd test && go test -v -timeout 45m ./...# a scheduled sweep job reaps anything a failed run leaked
Reliability and gating
Two more practical concerns. Reliability: infra tests can flake on cloud hiccups, so build in retries at the assertion level (not test-level reruns that mask real bugs), and keep tests isolated so a flake in one does not cascade. Gating: because the suite is slow and costly, many teams run the full Terratest suite on module PRs and nightly, while fast static checks gate every commit — a pragmatic split that keeps the pyramid’s economics working in CI.