Monday, August 13, 2018

HashiCorp Terraform 0.12 Preview: Template Syntax



----
HashiCorp Terraform 0.12 Preview: Template Syntax
// Hashicorp Blog

This is the seventh 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 the addition of the template syntax.

Terraform has always supported basic string interpolation using the ${...} syntax. Terraform 0.12 extends this syntax to support loops and conditionals that work directly within any value in a Terraform configuration.

Template Syntax

We've recognized that more complex templating is a common requirement of Terraform configurations. Terraform 0.12 introduces a new template syntax that is usable for any value.

The string interpolation syntax ${ ... } has been part of Terraform since its initial release in 2015. This continues to work in Terraform 0.12, but is now extended to include support for conditionals and for expressions. These new features are most useful within multi-line strings:

```hcl

Configuration for Terraform 0.12

locals { lbconfig = <<EOT %{ for instance in opccomputeinstance.example ~} server ${instance.label} ${instance.ipaddress}:8080 %{ endfor } EOT } ```

After evaluating the above, the expression local.lb_config would evaluate to a string like the following:

hcl server example0 192.168.2.12:8080 server example1 192.168.2.65:8080 server example2 192.168.2.23:8080

These new capabilities can be used for any value in Terraform. Because Terraform automatically converts values to their correct type, templates can even be used for numbers, booleans, and more.

These new capabilities are also available in the template_file data source after upgrading to the Template provider v2.0.0 release or newer. The template provider has many additional use cases and will continue to be supported, but we expect that most users will be able to use the new template syntax directly from Terraform 0.12 onwards.

If we had the freedom to start fresh with our template syntax we would ideally have used an existing template syntax, but since compatibility with the existing interpolations was a requirement we instead defined this new syntax in the spirit of our existing interpolation syntax while taking cues from the behaviors of other template languages. The template_file data source may still get support for other template engines in subsequent releases.

Upgrade Guide

This feature introduces no breaking changes. Existing string interpolation remains unchanged and the existing template provider and resources continue to work as they do in current Terraform versions.

Next

This was part 7 of the blog post series previewing Terraform 0.12.

The new template syntax will be released in Terraform 0.12, coming later this summer. To learn more about how to upgrade to Terraform 0.12, read the upgrade instructions which will be continuously updated as we get closer to releasing Terraform 0.12. If you have any feedback or concerns about these changes, please communicate with the Terraform team via the public mailing list.


----

Read in my feedly


Sent from my iPhone

No comments:

Post a Comment