-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function(s) to merge a list (or map) of maps/objects to a single map/object #23284
Comments
Hi @tomalok! Thanks for sharing this use-case. The Terraform language has a general feature for turning lists/tuples into multiple arguments, by using the special symbol
Because function arguments are ordered, this only works with sequence types (lists and tuples) and not for maps. However, if lexical sorting by key is sufficient for what you need -- which it sounds like should be true in your case where the different maps/objects have distinct keys/attributes -- you can use the
Does that seems like it would meet the use-case you've shared here? Since you're describing behaviors of Terragrunt's language rather than Terraform's language, we're not totally sure that the above would work there, but since Terragrunt is also using HCL as its foundation and this |
I'm not able to extract a list of keys from a set of service configurations. I can get something 'resembling' a map:
By applying this code:
But if I try to get just the service name (key) from that list, I get:
|
@teamterraform thanks for the tip of |
@teamterraform - verified that
|
Great, thanks for confirming @tomalok! In that case, we're going to close this out with the recommendation of using For future readers: note that the file we were discussing here was actually a Terragrunt configuration file rather than a Terraform configuration file, and thus not actually parsed by Terraform at all. It happens that the Terragrunt configuration file uses the same underlying expression language that Terraform does and so a Terraform answer was applicable to Terragrunt also in this case, but in general if you have feedback about the |
NOTE: This functionality is broken in some cases when trying to expand lists created using a for expression: #22404 |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Current Terraform Version
Use-cases
I have a dynamically-generated map of maps (or list of maps), and I'd like to merge them all down to a single map, using standard
merge(map1, map2, ...)
rules. FWIW, usingmerge()
with objects also seems to work, as it's a shallow merge, so I'd like to keep that behavior, too.I use these for picking up hierarchical variables from my directory structure (using
terragrunt
) -- if the same key is present when merging the maps, the one nearest to the local directory wins.Attempted Solutions
A
plan
results in:Proposal
merge(...)
accepts two or more maps as individual parameters, but does not accept a single parameter of map-of-maps or list-of-maps. If it's not possible to overload this function to allow for that, another function (or two) would take one or more maps-of-maps or lists-of-maps as parameters.The text was updated successfully, but these errors were encountered: