Why test infrastructure code

The deploy-assert-destroy idea.

Advanced12 min · lesson 1 of 12

Terratest is a Go library for testing infrastructure code the way it actually behaves: by deploying it for real, asserting on the result, and tearing it down. Static analysis (Checkov) tells you a manifest looks right; Terratest tells you the infrastructure works — the server actually responds, the module really creates a usable VPC, the Helm chart brings up a healthy service. It is integration testing for IaC, and it catches the class of bugs that only appear when code meets a real cloud.

The pattern is always the same three phases: deploy (apply the Terraform, install the chart), validate (make real assertions — hit the endpoint, query the API, check the output), and destroy (clean up everything). Because it uses real infrastructure, it is slower and costs money, so it sits at the top of the testing pyramid — fewer, higher-value tests than the fast static checks — but it is the only thing that proves the infrastructure genuinely functions.

The deploy-assert-destroy loop
1deploy
terraform apply / helm install
2validate
assert on the real result
3destroy
tear it all down
Real resources, real assertions. Slower and costlier than static checks — so used for the high-value cases.

Why real deploys

Some things simply cannot be checked without deploying: does the load balancer actually route to healthy backends, does the IAM role really let the app read that bucket, does the module compose correctly with its dependencies, does an upgrade path work. These are behavioral, integration-level questions. Terratest answers them by exercising the real thing in a throwaway environment, giving you confidence a static scan cannot.

Terratest deploys real, billable infrastructure
Unlike a linter, Terratest spins up actual cloud resources that cost money and take time, and a test that fails to clean up leaks those resources (and their bill). Everything in this course about defer-destroy, isolation, and CI cleanup exists because the tests are real deploys. Treat a Terratest suite as something that touches a real (ideally sandbox) account, not a free static check.