-
-
Save msuzoagu/5db1940347aaa118babf7b2bfe6dcaab to your computer and use it in GitHub Desktop.
The Terraform group_by you've been missing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The Terraform group_by you've been missing | |
| I'm playing around a lot nowadays with Terraform 0.13 and I found a really interesting | |
| feature and that's the `...` operator for for expressions. | |
| The operator can be used for group_by operations. | |
| ## Example | |
| We have a list of entries. The list contains employee/manager/project triplets. | |
| ```hcl | |
| locals { | |
| input = [ | |
| { employee = "Pete", manager = "Joe", project = "DEF" }, | |
| { employee = "Paul", manager = "Joe", project = "ABC" }, | |
| { employee = "Paul", manager = "Joe", project = "DEF" }, | |
| { employee = "Pam", manager = "Jim", project = "ABC" }, | |
| ] | |
| } | |
| ``` | |
| The goal is to group the list by either one of the attributes. | |
| First you'd try to go check terraform built-in function for group_by, but after an hour of grokking | |
| the terraform documentation you realize that terraform doesn't have a built-in group-by function | |
| for a list of maps/objects. You start to give up. | |
| BUT!!! | |
| You CAN do group_by with terraform! | |
| This is how you do a group by project: | |
| ```hcl | |
| output employee_projects { | |
| value = { | |
| for item in local.input : | |
| item.employee => item.project... | |
| } | |
| } | |
| ``` | |
| The crucial part is the `...`. This feature is hidden deep in the documentation of the | |
| [for expression](https://www.terraform.io/docs/configuration/expressions.html#for-expressions) | |
| > Finally, if the result type is an object (using { and } delimiters) then the value result expression can be followed by the ... symbol to group together results that have a common key: | |
| {for s in var.list : substr(s, 0, 1) => s... if s != ""} | |
| And that's how you group_by in Terraform! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| locals { | |
| input = [ | |
| { employee = "Pete", manager = "Joe", project = "DEF" }, | |
| { employee = "Paul", manager = "Joe", project = "ABC" }, | |
| { employee = "Paul", manager = "Joe", project = "DEF" }, | |
| { employee = "Pam", manager = "Jim", project = "ABC" }, | |
| ] | |
| } | |
| output project_employees { | |
| value = { | |
| for item in local.input : | |
| item.project => item.employee... | |
| } | |
| } | |
| output employee_projects { | |
| value = { | |
| for item in local.input : | |
| item.employee => item.project... | |
| } | |
| } | |
| output manager_employees { | |
| value = { | |
| for item in local.input : | |
| item.manager => item.employee... | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment