Terraform test

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.

Terraform code development lifecycle

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 code written in Go

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:

Unified language

HCL language is now the only language to learn as a DevOps to create various things, from an EC2 instance to a Spotify Playlist.

resource "spotify_playlist" "playlist" {
  name        = "Terraform Summer Playlist"
  description = "This playlist was created by Terraform"
  public      = true

  tracks = [

A Spotify playlist created with Terraform

Tests can now be written using HCL, enabling DevOps teams to design unit and acceptance tests using a consistent language and centralized location.

Shift left

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.

Since 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.

Starting 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!

Impact of Terraform test on Terraform code development lifecycle

Hashicorp recommend using:

  • preconditions for assumptions (For example, an aws_instance configuration can have the assumption that the given AMI will always be configured for the x86_64 CPU architecture.)
  • using postconditions for guarantees (For example, an aws_instance configuration 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


Tests - Configuration Language | Terraform | HashiCorp Developer
Write structured test code for validating your configuration.
Upgrading to Terraform v1.6 | Terraform | HashiCorp Developer
Upgrading to Terraform v1.6
Custom Conditions - Configuration Language | Terraform | HashiCorp Developer
Check custom requirements for variables, outputs, data sources, and resources and provide better error messages in context.
Create a Spotify playlist with Terraform | Terraform | HashiCorp Developer
Create a Spotify playlist with Terraform and share it with your friends.

Read more