After several months of development, Hashicorp unveiled Terraform version v1.6.0 on October 4, 2023. While this isn't a major release, it introduces a notable feature: the
terraform test command. In reality, this goes beyond just a command; it's an entire framework.
The dark age of infrastructure testing
Before talking about Terraform test framework and how this is a complete game changer to the infrastructure code development process, let's talk about the current situation: infrastructure testing is done once infrastructure is deployed.
This introduce lots of hidden costs: provisioning infrastructure isn't without cost, takes time to provision and destroy, it produces vast amounts of logs that also incur charges, and so on.
Terratest is one of the most popular framework used to match this need. Written in Go, Terratest require a little background in software development to apprehend the power of GoLang and create re-usables modules to test your infrastructure, a skill not typically associated with DevOps or SREs.
A new hope
Earlier in this post, I was talking about how Terraform test framework is a complete game changer to the infrastructure code development process. The impact is double:
HCL language is now the only language to learn as a DevOps to create various things, from an EC2 instance to a Spotify Playlist.
Tests can now be written using HCL, enabling DevOps teams to design unit and acceptance tests using a consistent language and centralized location.
Given that it's simpler and more cost-effective to patch code in development than in production, the "shift left" testing approach advocates for early-stage testing in the development process. By using regular and automated tests, the ultimate goal is to eradicate bugs and improve code quality.
Terraform v1.2.0, you can use lifecycle block postconditions to run checks after planning and applying changes to a managed resource, or after reading from a data source.
Terraform v1.6.0 and the introduction of Terraform test framework, tests can be run before applying changes to your infrastructure, saving time and costs!
Hashicorp recommend using:
- preconditions for assumptions (For example, an
aws_instanceconfiguration can have the assumption that the given AMI will always be configured for the
- using postconditions for guarantees (For example, an
aws_instanceconfiguration can have the guarantee that an EC2 instance will be running in a network that assigns it a private DNS record.)
In addition, Terraform test framework can be used for Test Driven Development, introducing a new way to manage infrastructure quality.
Lenstra helps companies leverage Computer Science to enhance their Economic Performance
Contact us for a free consultancy to explore how we can work together.Contact us