HashiCorp recently made Terraform 1.6 generally available. Let’s get into it!
terraform test
Now module maintainers can write tests for Terraform native to HCL.
I’ll be writing a separate, deeper-dive article on the ins and outs of terraform test syntax, but for now, the basic highlights are:
- Create tests defined in HCL within a
.tftest.hclfile. HashiCorp has a good simple example that can be viewed here. - Run
terraform testwhich provisions resources based on the test definitions, checks the configuration against required assertions, and then tears down all cloud resources.
This provides a clean way to test that a particular module of Terraform code behaves as expected, and is a large improvement over other ways of testing Terraform modules which include:
- Testing in dev: Deploy new module changes within a development environment prior to releasing to production and ensure functionality is as expected. While this does work, when bugs occur it can result in a very slow cycle time while making corrections.
- Custom Ephemeral Pipelines: Build a custom CI pipeline to create, assess, and destroy resources created by a Terraform module. If this sounds like it would be difficult to set up robustly, that is because it is.
- Third Party Libraries: Third party tools like Gruntwork’s terratest allow for defining tests of Terraform configuration using GoLang, which can help make building a “Custom Ephemeral Pipeline” easier. That being said, writing tests for HCL using GoLang requires a lot of context switching and is not the simplest process.