BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Terraform 1.9 Released with Enhanced Input Validation and New String Template Function

Terraform 1.9 Released with Enhanced Input Validation and New String Template Function

HashiCorp has announced the general availability of Terraform 1.9, introducing several new features and improvements to enhance developer productivity and code reliability. This version of the infrastructure-as-code tool is now available for download and use in HCP Terraform.

One of the key enhancements in Terraform 1.9 is the expansion of input variable validations. This feature, first introduced in Terraform 0.13, allows developers to ensure that input variable values meet specific requirements before execution. Previously, the condition block of an input validation could only refer to the variable itself. With the new release, conditions can now reference other input variables, data sources, and local values, significantly broadening the scope of validation scenarios.

This enhancement enables more complex and dynamic input validations, reducing the likelihood of provisioning errors and misconfigurations caused by invalid or unexpected user input. For example, developers can now create validations that check the relationship between multiple variables or validate input against dynamically retrieved data.

To illustrate this new capability, HashiCorp provided an example where a variable validation condition references the value of a different variable:

In this scenario, if a user attempts to execute the Terraform configuration with create_cluster set to false without providing a value for cluster_endpoint, they will encounter a validation error. This cross-referencing validation was previously only possible using precondition blocks on data sources or resources, which could lead to partially completed deployments.

Writing on Medium, Babula Parida explains how this validation can be used.

On X, Mark Tinderholt explains more of the possibilities:

"This thing has the potential to help you validate every nook and cranny from CPU and Memory requirements, Accelerated Networking to Number of supported disks / NICS as well as the ever-murky combinations of supported Virtual Machine / Disk SKUs!!!" - Mark Tinderholt

Tinderholt also announced a Terraform module to make this easy.

Another significant addition in Terraform 1.9 is the new "templatestring" function. This built-in function is designed to render templates obtained dynamically, such as from a data source result, without saving them to a file on the local disk. The function takes two parameters: a direct reference to a named string object in the current module and an object representing the templated variables to interpolate.

This new function is handy for retrieving templates from external locations, transforming them, and passing them to other resources. HashiCorp provided an example demonstrating how to use "templatestring" to retrieve a Kubernetes resource manifest template from an HTTP location, inject input variables and resource references, and then use it in a kubernetes_manifest resource.

Terraform 1.9 also includes improvements to existing features. The deprecated null_resource type in the hashicorp/null provider can now be refactored directly to the new terraform_data resource type using moved blocks. This enhancement builds on the cross-type refactoring feature introduced in Terraform 1.8 and allows developers to modernize their code more easily.

Additionally, removed blocks can now declare provisioners to be executed when the associated resource instances are destroyed. This feature is helpful in scenarios where developers want to remove the resource declaration from their configuration but still execute the destroy-time provisioner.

Users interested in exploring the new features can download Terraform 1.9 or sign up for a free HCP Terraform account. HashiCorp has also provided an upgrade guide for those moving from previous versions and offers hands-on tutorials through HashiCorp Developer.

As with previous releases, HashiCorp acknowledged the role of community feedback in shaping the new features and improvements in Terraform 1.9. The company encouraged users to continue providing feedback through GitHub issues, HashiCorp Discuss forums, and direct customer interactions to help guide future development of the tool.

About the Author

Rate this Article

Adoption
Style

BT