What’s new in Terraform 1.6: Testing!
<p>HashiCorp recently made <a href="https://github.com/hashicorp/terraform/releases/tag/v1.6.0" rel="noopener ugc nofollow" target="_blank">Terraform 1.6</a> generally available. Let’s get into it!</p>
<h1>terraform test</h1>
<p>Now module maintainers can write tests for Terraform native to HCL.</p>
<p>I’ll be writing a separate, deeper-dive article on the ins and outs of <code>terraform test</code> syntax, but for now, the basic highlights are:</p>
<ol>
<li>Create tests defined in HCL within a <code>.tftest.hcl</code> file. HashiCorp has a good simple example that can be viewed <a href="https://developer.hashicorp.com/terraform/language/tests?ajs_aid=e20adfa8-4462-4806-9490-bdf9b49895cc&product_intent=terraform#example" rel="noopener ugc nofollow" target="_blank">here</a>.</li>
<li>Run <code>terraform test</code> which provisions resources based on the test definitions, checks the configuration against required assertions, and then tears down all cloud resources.</li>
</ol>
<p>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:</p>
<ul>
<li><strong>Testing in dev</strong>: 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.</li>
<li><strong>Custom Ephemeral Pipelines: </strong>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.</li>
<li><strong>Third Party Libraries</strong>: Third party tools like Gruntwork’s <a href="https://github.com/gruntwork-io/terratest" rel="noopener ugc nofollow" target="_blank">terratest</a> 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.</li>
</ul>
<p><a href="https://medium.com/@hello_9187/whats-new-in-terraform-1-6-testing-9d3fbf59f3a8"><strong>Visit Now</strong></a></p>