BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Terraform 1.6 Makes Testing Framework Generally Available

Terraform 1.6 Makes Testing Framework Generally Available

This item in japanese

HashiCorp has released Terraform 1.6 with several new improvements including a new testing framework. Additional improvements include changes to config-driven import, Terraform Cloud CLI workflows, and the Amazon S3 backend. This version marks the first release of Terraform to be under the Business Source License v1.1 (BSL 1.1).

The testing framework allows for the creation and running of tests to validate the functionality of the Terraform configurations. These tests are also written in the HashiCorp Configuration Language (HCL) syntax used by Terraform. A sequence of run blocks is used to craft unit and integration tests with assertions against various inputs and provider configurations.

The following example creates an AWS S3 bucket using an input variable as the bucket name:

# main.tf

provider "aws" {
    region = "eu-central-1"
}

variable "bucket_prefix" {
  type = string
}

resource "aws_s3_bucket" "bucket" {
  bucket = "${var.bucket_prefix}-bucket"
}

output "bucket_name" {
  value = aws_s3_bucket.bucket.bucket
}

The following test file runs a plan command to create the bucket and then validates that the bucket name matches what is expected:

# valid_string_concat.tftest.hcl

variables {
  bucket_prefix = "test"
}

run "valid_string_concat" {

  command = plan

  assert {
    condition     = aws_s3_bucket.bucket.bucket == "test-bucket"
    error_message = "S3 bucket name did not match expected"
  }

}

The run block supports several fields and blocks including both apply and plan. Optionally, variables, modules, and providers can be specified as input to the test.

Tests differ from validations in a few subtle ways. Validations are executed during the plan and apply operations whereas tests are only executed by running terraform test. However, the test command will also run any existing validations. Validations are also exposed to module users, so failure messages should be crafted accordingly.

The new test framework deprecates and replaces the experimental test command introduced in Terraform version 0.15. The v1.6.x upgrade guide provides a review of changes between the experimental and final commands.

This release expands upon the config-driven import feature released in Terraform 1.5. This feature provides a means of importing existing resources into Terraform. In version 1.5 the import blocks id attribute only supported a static value. With this release, the id attribute now accepts expressions referring to other values, including support for string interpolation.

import {
  # You can now provide the import ID via a variable
  id = var.bucket_name
  to = aws_s3_bucket.example
}
 
import {
  # The aws_s3_bucket_versioning resource includes the AWS account in its import ID
  id = "${var.bucket_name},${data.aws_caller_identity.current.account_id}"
  to = aws_s3_bucket_versioning.example
}

The Amazon S3 remote state backend has also been better aligned with the AWS SDK and the Terraform AWS provider. This includes moving some attributes into nested blocks, support for environment variables, and some new options for customizing AWS service endpoints.

Users of Terraform Cloud are now able to save their cloud plans to be applied later. This new run mode works with the Terraform 1.6 CLI or the Terraform Cloud API.

Response to the release was mixed on social media. Many users praised the addition of the new testing framework. However, others were disappointed that this release coincided with the recent licensing change to use BSL 1.1.

Terraform 1.6 is available now from GitHub. More details about the release can be found on the HashiCorp blog, the upgrade guide, and in the changelog.

About the Author

Rate this Article

Adoption
Style

BT