Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Use Go Template as the template syntax for template_file data source #16628
This is an enhancement request to change the template_file data source to use Go Template as its template syntax. This, of course, is familiar within HC suite for having been used within consul-template.
I believe this can help address a number of issues related to resource generation and nested looping (eg. #8573) and dynamic generation/manipulation of nested variables (map-of-maps, etc) while at the same time avoiding complexity in the TF core brought about by other proposals (such as #5278.) The template_file data source interface definition would not need to change to accommodate this. Within the template file itself one could still only have access to the variables defined in the vars section (as it is today) but have at hand the greater power of the Golang Template constructs (looping, conditionals, etc) for the actual rendering of the template.
Nested loops can be achieved with template_file as it exists today though only for simple cases. Here's one example:
Hi @russroy! Thanks for this suggestion.
We are aware of the limitations of the current template language. In fact, calling it a template language is generous, since it's really just the interpolation language from the rest of Terraform.
However, we do not plan to use the Go template language for this because it introduces a number of Go-specific ideas that are somewhat alien to Terraform, and in particular can't support Terraform's expression type system without heavy customization.
Instead, we are planning to extend the interpolation language to be a true template language as part of the currently-in-progress configuration language revamp. We decided to take this direction because it then also allows these new features to be used within interpolation strings in the main configuration too, and ensures that values will behave the same way in the main configuration and in templates. This is coming as part of the new parser that is currently code-named HCL2 (will become just "HCL" again when it's stable) and you can read about the new template syntax -- an extension of the existing interpolation syntax -- in its specification.
The work to integrate this new parser is in progress and will appear iteratively over a few different releases in the near future.
@serverhorror I wrote a little thing about looping with the existing implementation - maybe this will help https://medium.com/ovni/terraform-templating-and-loops-9a88c0786c5c
also as I wasn't going to wait and actually quite like the idea of having actual