DEV Community

Philip LaVoie
Philip LaVoie

Posted on

Some Neat Enhancements in HashiCorp's Terraform 0.12 Preview

Hello World,

I've been digging in a bit more as the release approaches, and just wanted to share some things I'm excited about in the Terraform 0.12 preview.

First, let's have a quick overview/recap of the Terraform Configuration Language in 0.11 to set us up for talking about the new goodies...

The Terraform Configuration Language in 0.11 is made up of a couple distinct parts:

  1. HCL - HashiCorp Configuration Language - the block and attribute syntax HCL Example
  2. HIL - HashiCorp Interpretation Language - Allows us to embed expressions into HCL strings HIL Example
    • This includes Terraform native variables and functions - Terraform ships with built-in variables and functions.
      • Functions are called with the syntax name(arg, arg2, ...). For example, to read a file: ${file("path.txt")}.

So Terraform 0.12 introduces HCL 2, and this is a complete rewrite of that underlying configuration language. It has enhanced the HCL in the following ways:

  • It merges the HCL and HIL
  • It introduces a robust type system
  • It includes a large list of feature requests and enhancements that the community had been asking for.

These are just some of the changes that are inbound:

  • Reliable JSON Syntax
  • For and For-Each Expressions
  • Generalized Splat Operator
  • First-Class Expressions
  • Conditional Operator Improvements and Conditionally Omitted Arguments
  • Module Input and Output Rich Value Types
  • Template Syntax
  • Dynamic Blocks
  • Improved Error Messages

As awesome as the list has become, these are some of the HCL enhancements I'm really excited about...

  1. First-class expressions: Previously these had to be wrapped in interpolation sequences with double quotes, such as "${var.foo}". This changes in the 0.12 preview, expressions are now a native part of the language and can be used with raw dot notation as you would in other programming languages.

    • Examples:
      • foo = var.foo
      • ami = var.ami[1]
  2. Module Input and Output Rich Value Types: Terraform has supported basic lists and maps as inputs/outputs since Terraform 0.7, but elements were limited to only simple values. Terraform 0.12 allows arbitrarily complex lists and maps for any inputs and outputs, this includes use with modules.

    • Terraform 0.12 can now represent the individual types of a complex object. This was not possible in earlier versions of Terraform. The example below shows a type specification and the object can also be passed as both input and output, to and from, a module. You may also notice another difference where the types are not quoted... Types are now first-class values in Terraform 0.12 and can be used directly. Complex Type
    • Relatedly, you can also output entire resources from modules now. This does away with round trips back to the module creator/maintainer to expose other values as outputs to consumers: Resource Output
  3. Improved Error Messages: This is hands-down my favorite though, and here's why...

    • This is a Terraform 0.11 error message. The main.tf could be thousands of lines long and we wouldn't know what closing brace was missing. This wasn't helpful and most of us spent time commenting chunks of code out until some editor syntax highlighting extensions became available. v11 Error
    • So in 0.12 they've stepped up the game. Here's a Terraform 0.12 error message. The HCL2 parser retains detailed information about the location of any syntax elements or tokens, so not only will you now get what file this error is from, we get a line number and it prints out the line in question! v12 Error
    • Another example they have demonstrated, here we have type checking enforcement raising an error when trying to add a number to string. Good Ol TypeMismatch letting us know what the issue is, and it will tell us exactly the file and the line. TypeMismatch
    • What I ♥️ about this last one that they demo'd was the git feeling of the message! Yes, that's exactly what I meant, thanks for looking out for us! Git Style Suggestion

So those are my top 3 features that are appearing. But honestly, the entire set of enhancements makes this so much more than a mild upgrade that I'll be looking forward to implementing.

Feel free to peruse more listed here on the HashiCorp Terraform Blog: https://www.hashicorp.com/blog/category/terraform

Most are prefixed with "HashiCorp Terraform 0.12 Preview:" and they are posting more as the release approaches.

Happy Coding!

Top comments (0)