Converting a list to a string

9,736 views
Skip to first unread message

Andrew Langhorn

unread,
Nov 21, 2016, 8:57:30 AM11/21/16
to terrafo...@googlegroups.com
Hi,

The aws_emr_cluster resource's ec2_attributes block accepts a subnet_id. I have a subnet per availability zone, and subnet_id accepts only one value.

In order to spin up a cluster in any of my available subnets, I was looking at using:

resource "random_shuffle" "az" {
  input = ["private-a", "private-b", "private-c"]
  result_count = 1
}

resource "aws_emr_cluster" "cluster" {
  ...
  ec2_attributes {
    ...
    subnet_id = "${random_shuffle.az.result}"
    ...
  }
}

This returns a list, rather than a string, which subnet_id expects. Is there a way to convert a list to a string?

Thanks,

Andrew

David Adams

unread,
Nov 21, 2016, 10:37:48 AM11/21/16
to terrafo...@googlegroups.com
I'm guessing that either:

    subnet_id = "${random_shuffle.az.result[0]}"

Or if not that, then:

    subnet_id = "${element(random_shuffle.az.result, 0)}"



--
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/CAEpa1DL%3DONdcOcMOT722eLSPQtCrQvViuoXiDL%3DLvHBsXSK%2BjQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Langhorn

unread,
Nov 21, 2016, 11:18:35 AM11/21/16
to terrafo...@googlegroups.com
Ahhh, thanks. I'd previously tried element, but that didn't do the trick. But referring to the first item in the list that way worked a charm.

:)

edw...@getdinghy.com

unread,
Sep 3, 2017, 9:00:34 AM9/3/17
to Terraform, andrew....@thoughtworks.com
The problem with the solution provided here is you're discarding any other items in the list.

Better to use the following function to convert the list directly to a string and keep everything.

https://www.terraform.io/docs/configuration/interpolation.html#join-delim-list-

e.g.
Reply all
Reply to author
Forward
0 new messages