Accessing Nested Maps

1,466 views
Skip to first unread message

Matthew Ceroni

unread,
Oct 20, 2017, 3:23:15 PM10/20/17
to Terraform
I have the following structure

  workers =
  [
    {
      auto_scaling.min     = "1"
      auto_scaling.max     = "6"
      auto_scaling.desired = "1"
      labels = {
        namespace = "default"
        role             = "testing"
      }
    }
  ]

As you can see, I have an array of maps with two nested maps inside. The first being auto_scaling. After some googling I found the . trick such that I could do ${lookup(var.workers[count.index], "auto_scaling.min")} to return the value vs trying to do two lookups which doesn't work as lookup only works with flat items.

However, that above trick doesn't work for the labels component because I need to be able to access the map as a whole to pass it to jsonencode. I have tried various things but haven't found a solution yet so wondering if anyone else out there can think outside the box and come up with a solution.

The above variable is actually used to spin up different Auto-Scaling groups and the labels are then passed into as user data such that during provisioning the node can be labled with those labels (kubernetes nodes).

Thanks

Anshu Prateek

unread,
Oct 20, 2017, 11:30:57 PM10/20/17
to terrafo...@googlegroups.com

Maybe define flat maps first and then create nested map at runtime ?

something like:

workers = ["${merge("${var.map_1}","${map("labels","${var.map_2}")}")}"]



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/cbc93362-5d73-4b5f-9815-2b81670dd1fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
regards
Anshu Prateek
+91.991.610.2967

Anshu Prateek

unread,
Oct 20, 2017, 11:32:14 PM10/20/17
to terrafo...@googlegroups.com
terraform console
> var.map_1
{
  auto_scaling.max = 6  auto_scaling.min = 1}
> var.map_2

{
  namespace = default  role = testing}
> "${merge("${var.map_1}","${map("labels","${var.map_2}")}")}"
{
  auto_scaling.max = 6  auto_scaling.min = 1  labels = {    namespace = default  role = testing}}




Reply all
Reply to author
Forward
0 new messages