Managing infrastructure has always been a challenging mission for DevOps teams. In order to maintain a flattened and precise template, it took many working hours. The tedious and error-prone processes of managing infrastructure led to the idea of Infrastructure as Code also known as IaC.
IaC is the process of managing and provisioning infrastructure through a declarative language that simplifies the way you interact with your resources.
Hashicorp’s Terraform, one popular and powerful IaC platform, helps with provisioning and creating an infrastructure form code that supports any cloud provider.
Terraform, which can be easily learned, is written in HCL (HashiCorp configuration language) and is a declarative language.
Over time HashiCorp has had to upgrade the HCL language to give their users more abilities and new features.
In this blog post, we’ll cover the new features and tips and tricks of Terraform 0.12 and HCL2 that will enable you to understand how it works and write your templates using best practices.
Nested Blocks and Maps
Let’s get started with the differences between attribute Blocks and Maps.
The new version of Terraform has begun to be strict and you can’t mix these two.
So when you will be upgrading your Terraform files, it’s the upgrade tool that will change your template.
Example of Nested Blocks
Loops in Nested Blocks
This feature was heavily requested by DevOps and was released in this version of Terraform.
In the old version of Terraform, loops weren’t integrated with the HCL language and you would need to have workarounds and tweaks in order to get the same result.
In HCL 2 you can add resources dynamically and that will generate, behind the scenes, the list of resources.
In the old version of Terraform, you would have to surround your variables in quotes when pointing to variables and other resources.
Now you will just point to those variables directly.
In addition, when referring to lists and object you can call them directly instead of referring to each of the parameters on their own.
Tip: In the migration process, the upgrade tool doesn’t know how to change lists correctly.
Will transform into that:
So to achieve the below (correct) results, you will need to do this part of the upgrade manually.
We have covered many of the main features of Terraform 0.12 in this blog post. To see all of the new features, read more here:
Examples – https://github.com/rberlind/terraform-0.12-examples
Official change log – https://www.terraform.io/upgrade-guides/0-12.html