AUG 23 2018    MARTIN ATKINS

This is the eighth post of the series highlighting new features in Terraform 0.12.

As part of the lead up to the release of Terraform 0.12, we are publishing a series of feature preview blog posts. The post this week is on bringing a clear 1:1 mapping between JSON and HCL.

Terraform has accepted JSON as configuration in addition to HCL since version 0.1. However, the implementation of this prior to version 0.12 had many shortcomings. The HCL improvements in Terraform 0.12 include a clear 1:1 mapping between HCL and JSON, including much clearer error messages and support for comments!

These improvements make it easy to work with Terraform using HCL, JSON, or both.

HCL and JSON

Terraform has supported JSON as an alternate configuration format since its initial release in Terraform 0.1. Further, Terraform has supported mixingJSON and HCL configurations within the same module since version 0.1, as well. Both JSON and HCL are important first-class configuration formats for Terraform.

The JSON mapping in prior versions of Terraform had many shortcomings. In some cases, it felt as though JSON didn't feel like a first class solution in the tool. We have always considered JSON a first class input to Terraform and continue to do so, so a big part of Terraform 0.12 was having a clean 1:1 mapping between HCL and JSON, and ensuring every feature of HCL is supported in JSON.

Terraform 0.12 achieves these goals. In future versions of Terraform, we will also support native tooling to convert HCL to JSON and JSON to HCL cleanly (including comments). This gives additional tooling more power in working with Terraform configurations and also enables people to author configurations in whichever language they feel comfortable.

Error Message Improvements

In versions prior to 0.12, the configuration mapping to JSON was only weakly specified in documentation, there were many counter-intuitive mapping behaviors, and there was a tendency to silently ignore misshapen constructs that made debugging difficult.

Terraform 0.12 includes a new JSON configuration implementation that is more robust than prior versions to address these limitations. In particular, the JSON-based configuration decoder produces errors in any cases where the input cannot be mapped exactly onto the expected configuration structure.

The example below shows an invalid configuration the error produced in Terraform 0.12: