Roger Berlind
Mar 17 · 3 min read

HashiCorp recently released Terraform 0.12 Beta 1 which includes major Terraform language enhancements that make it easier for customers to implement their infrastructure as code with Terraform. This blog post guides you through a repository of examples I wrote that demonstrate most of the language enhancements in Terraform 0.12. The examples will teach you how to use the new language features and help you unleash the power of Terraform 0.12 in your own Terraform configurations.

Prerequisites

I recommend you first read the Terraform 0.12 Preview blog post since it summarizes the notable improvements in HCL and Terraform that have been added to the 0.12 release. You will need to download and install Terraform 0.12 to run the examples, but the repository’s README.md gives detailed instructions. You will also need an AWS account to run the first four examples.

Language Improvements Covered

The repository covers the following Terraform language improvements:

  • First class expressions
  • For expressions
  • Conditional expressions with lists
  • Dynamic blocks
  • Splat expressions
  • Rich value types
  • String templates
  • JSON error handling
  • JSON comments

The Examples

The First Class Expressions example illustrates referencing Terraform variables and resource arguments without interpolation. It also illustrates how = must now be used when setting values for maps such as AWS tags.

The For Expressions example shows how the new for expression can be used to iterate across multiple items in lists and maps. It generates an output that gives the private IP addresses of some EC2 instances in several ways, using the old splat expression .*, the new splat expression [*], and the for expression, illustrating that they all give the same result. It also shows how the for expression can be used with lists in conditional expressions in two ways.

The Dynamic Blocks and Splat Expressions example shows how multiple blocks of the same type can be dynamically created with a single dynamic block and then referred to in an output using the generalized splat expression. The input to the dynamic block is a variable of type list.

The Rich Value Types example illustrates how the new rich value types can be passed into and out of modules. These types encompass standard types as well as resource types. This example also shows that entire resources can be returned from a module. You can even return the entire module itself!

The New Template Syntax example shows how string templates can be used in Terraform code and in template files. This is implemented with the new %{ ... } directive which is combined with the for expression discussed above.

Finally, the Reliable JSON Syntax example demonstrates that Terraform 0.12 provides much better error handling for Terraform code written using Terraform’s JSON configuration syntax than older versions of Terraform did and supports comments in JSON configuration files.

Conclusion

I hope the examples help you learn and appreciate the language enhancements in Terraform 0.12. You can read more about the Terraform 0.12 language here. Additionally, the Terraform CLI includes an upgrade command for upgrading Terraform configurations to the new version. So, you should now be able to convert your existing Terraform code to 0.12 easily and quickly and understand any changes made by the upgrade command.

HashiCorp Solutions Engineering Blog

A Community Blog by the Solutions Engineers of HashiCorp and Invited Guests

Roger Berlind

Written by

Roger is a Sr. Solutions Engineer at HashiCorp with over 20 years of experience explaining complex technologies like cloud, containers, and APM to customers.

HashiCorp Solutions Engineering Blog

A Community Blog by the Solutions Engineers of HashiCorp and Invited Guests

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade