Some Neat Enhancements in HashiCorp's Terraform 0.12 Preview
Tags: devops, tools, technology
Published: 2019-01-04 21:06:22

---
title: Some Neat Enhancements in HashiCorp's Terraform 0.12 Preview
published: true
description: Some things I'm excited about in the Terraform 0.12 preview
tags: devops, tools, technology
---

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](https://www.terraform.io/docs/configuration/syntax.html) - the block and attribute syntax
    ![HCL Example](https://thepracticaldev.s3.amazonaws.com/i/9446fw4mvv5ro64m7dlk.jpg)
2. HIL - [HashiCorp Interpretation Language](https://www.terraform.io/docs/configuration/interpolation.html) - Allows us to embed expressions into HCL strings
    ![HIL Example](https://thepracticaldev.s3.amazonaws.com/i/germx2yb76zys8pzgmsb.jpg)
    - 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](https://www.hashicorp.com/blog/terraform-0-12-preview-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](https://www.hashicorp.com/blog/terraform-0-12-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](https://thepracticaldev.s3.amazonaws.com/i/47jolq3gthxixvg0q6ic.jpg)
    - 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](https://thepracticaldev.s3.amazonaws.com/i/jbg7e79ixxbxdejcc5s9.jpg)

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](https://thepracticaldev.s3.amazonaws.com/i/a6mm228evpqixc4f83v2.jpg)
    - 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](https://thepracticaldev.s3.amazonaws.com/i/2dqtaba9wfapz0l40zzh.jpg)
    - 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](https://thepracticaldev.s3.amazonaws.com/i/0bjhb8tmgbhf7fumx19w.jpg)
    - 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](https://thepracticaldev.s3.amazonaws.com/i/z66uxy6ufh28vhzjlvze.jpg)

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!