BT

Terraform 0.12 Releasing This Summer with for Loops and First-Class Expressions

| by Matt Campbell Follow 2 Followers on Aug 02, 2018. Estimated reading time: 2 minutes |

HashiCorp will be releasing Terraform 0.12 later this summer. This release includes a number of new, heavily requested improvements to Terraform based on community feedback and input. Most notably among the changes are: first-class expressions, the for expression, and conditionals. Upgrading to the new version may cause breaking changes for some users of Terraform.

First-class expressions will simplify writing Terraform scripts and also add additional benefits to the language. In the current version, expressions need to be wrapped in double quotes and are treated as interpolation sequences. With the release of 0.12, expressions are now a native part of the language and can be written directly:

variable "ami" {}

# Terraform 0.11 and earlier
resource "aws_instance" "example" {
   ami = "${var.ami}"
}

# Terraform 0.12
resource "aws_instance" "example" {
   ami = var.ami
}

This change also allows the usage of list and map syntax via [...] and {...} directly within expressions. Coupling this with the new for expression will allow for dynamic filtering and transformations of lists and maps within your configurations. An optional if clause can be included to allow for filtering of the result:

# Terraform 0.12

output "instance_public_ip_addresses" {
   value = {
      for instance in aws_instance.example:
         instance.id => instance.public
         if instance.associate_public_ip_address
   }
}

While the conditional operator ( … ? … : …) has been in Terraform for some time, its use was limited as both portions of the return value would always be evaluated. With the upcoming release, that has been corrected and the conditional operator now behaves as you would expect. The other major improvement is that the conditional operator can be leveraged with any type.

# Terraform 0.12

locals {
   first_id = length(azurerm_virtual_machine.example) > 0 ? azurerm_virtual_machine.example[0].id : ""
}

In current versions of Terraform the above example would fail as both return values are evaluated regardless of what the expression resolves to.

Introducing these changes into Terraform required a reworking of the underlying configuration language. While the Terraform Core Team states they have worked to minimize breaking changes, an upgrade guide has been prepared to assist with the move to version 0.12. It is recommended that you constrain the Terraform version in your scripts ahead of the release. They feel that the focus on configuration changes for this release will minimize breaking changes going forward as well as allow for quicker turnaround on future features. 

The Teraform team are continuing to blog about the upcoming changes and are looking for community feedback regarding these and future improvements.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT