Hi there,
is there any ability to get a sublist from a list? After data sources release (damn, they're so sweet) that become really desired functional.
For e.g. there is "aws_availability_zones" data source and we can easily allocate multi-zoned environment with interpolation, however in some cases there is 5 AZ available whereas only 2 required for particular project. Is there any way to do that or can we expect such feature soon?
Hi @dene14!
There isn't a direct way to do this right now, but I have some workarounds for you:
If you specifically want the first two in the list, and you know there are at least two (which for AWS Availability Zones seems like a reasonable assumption) then you can just list them out manually:
[
"${data.aws_availability_zones.available.names[0]",
"${data.aws_availability_zones.available.names[1]",
]
We also have the random_shuffle resource, whose initial use-case was to pick any two AZs from the available set:
resource "random_shuffle" "az" {
input = ["${data.aws_availability_zones.available.names}"]
result_count = 2
}
If you use this one, it's important to understand the constraints and caveats relating to the random provider.
With all of that said, a function for slicing a sub-list out of a list seems like a very reasonable request, so I'll leave this issue here as a request for that. :grinning:
👍 a slice interpolation function would do it for me
slice(begin_idx[, end_idx]) where an index can be negative to indicate counting back from the the end of the list, and end_index is optional to indicate slicing to the end of the list.
One way I've found of working around the lack of such functionality is to use the replace() function on a string representation of the list. For example, to remove the last ID from a list of subnet IDs:
"${split(",", replace(join(",", aws_subnet.foo.*.id), "/,subnet-[[:xdigit:]]+$/", ""))}"
join()replace() and the power of regular expressionssplit()BTW, not recommending this as in any way maintainable in the long term 😄.
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.
Most helpful comment
👍 a
sliceinterpolation function would do it for meslice(begin_idx[, end_idx])where an index can be negative to indicate counting back from the the end of the list, and end_index is optional to indicate slicing to the end of the list.