Chapter Eleven

Testing and Validation

The gates that keep Terraform code honest — from a two-second format check to native HCL tests, security scanners, policy as code, and the contract a shared module owes its consumers.

5 topics

Infrastructure code fails differently from application code. A typo does not crash a process you can restart — it deletes a database, opens a security group to the internet, or quietly drifts production away from what anyone reviewed. The cost of a bad change is paid in real resources, so the discipline is to catch problems before apply, when they are still text on a screen.

This chapter walks the testing pyramid for Terraform from cheapest to most thorough. fmt and validate run in seconds and catch structural mistakes. Static analysis flags the public bucket and the open port. The native terraform test framework asserts that a module does what it claims. Policy as code enforces your organization's rules on every plan. And contract testing keeps a widely-used module's interface trustworthy across versions. Each layer catches what the one below it cannot.

Topics in This Chapter