output map using lists

1,992 views
Skip to first unread message

Andy

unread,
Nov 8, 2016, 11:56:28 AM11/8/16
to Terraform
It seems like I am trying to do something that is not possible but I thought I would ask here since I am new to terraform.

I have this output that I want to consume in a module. If I try to access this in any way with the lookup function I will receive the errors:

lookup: lookup() may only be used with flat maps, this map contains elements of type list


output "test" {
  value
= "${
    map(
      "
test", ["one","two","three"]
    )
  }"

}


Is this just simply not possible or am I missing the proper way to access a list index with the lookup function?


Thanks
Andy

David Adams

unread,
Nov 8, 2016, 12:41:30 PM11/8/16
to terrafo...@googlegroups.com
lookup() can only return a string. So your map's value will need to be a string rather than a list. You can work around this by using commas (or something else) as a separator, then splitting:

    "${element(split(",",lookup(module.mod.test, "test")),count.index)}"

That may not be exactly right, but hopefully conveys the idea. This is an area that has gotten a lot of work, but is clearly pretty complicated to implement, so often you have to resort to what feel like hacks. But it's way better than it used to be :)

--
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-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/677b4536-18d3-4287-a073-9a1911492f4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy

unread,
Nov 8, 2016, 1:30:24 PM11/8/16
to Terraform
Ok I get it so one other thing...

Since I have tried to use a variable inside of this every which way and it will not work, can I assume it is also impossible?
${module.namespace.${var.test}}

So for example that is why I wanted to use the map lookup function instead of defining a bunch out individual outputs that return lists like so:

output "test" {
  value = ["one","two","three"]
}

#and can access like this
${module.namespace.test}[0]}



On Tuesday, November 8, 2016 at 12:41:30 PM UTC-5, David Adams wrote:
lookup() can only return a string. So your map's value will need to be a string rather than a list. You can work around this by using commas (or something else) as a separator, then splitting:

    "${element(split(",",lookup(module.mod.test, "test")),count.index)}"

That may not be exactly right, but hopefully conveys the idea. This is an area that has gotten a lot of work, but is clearly pretty complicated to implement, so often you have to resort to what feel like hacks. But it's way better than it used to be :)
On Tue, Nov 8, 2016 at 10:56 AM, Andy <andys...@comcast.net> wrote:
It seems like I am trying to do something that is not possible but I thought I would ask here since I am new to terraform.

I have this output that I want to consume in a module. If I try to access this in any way with the lookup function I will receive the errors:

lookup: lookup() may only be used with flat maps, this map contains elements of type list


output "test" {
  value
= "${
    map(
      "
test", ["one","two","three"]
    )
  }"

}


Is this just simply not possible or am I missing the proper way to access a list index with the lookup function?


Thanks
Andy

--
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.
Reply all
Reply to author
Forward
0 new messages