- The OpenTofu Language
- Functions
- setproduct
setproduct
Function
The setproduct
function finds all of the possible combinations of elements
from all of the given sets by computing the
Cartesian product.
This function is particularly useful for finding the exhaustive set of all combinations of members of multiple sets, such as per-application-per-environment resources.
You must pass at least two arguments to this function.
Although defined primarily for sets, this function can also work with lists. If all of the given arguments are lists then the result is a list, preserving the ordering of the given lists. Otherwise the result is a set. In either case, the result's element type is a list of values corresponding to each given argument in turn.
Examples
There is an example of the common usage of this function above. There are some other situations that are less common when hand-writing but may arise in reusable module situations.
If any of the arguments is empty then the result is always empty itself, similar to how multiplying any number by zero gives zero:
Similarly, if all of the arguments have only one element then the result has only one element, which is the first element of each argument:
Each argument must have a consistent type for all of its elements. If not, OpenTofu will attempt to convert to the most general type, or produce an error if such a conversion is impossible. For example, mixing both strings and numbers results in the numbers being converted to strings so that the result elements all have a consistent type:
Finding combinations for for_each
The
resource for_each
and
dynamic
block
language features both require a collection value that has one element for
each repetition.
Sometimes your input data comes in separate values that cannot be directly
used in a for_each
argument, and setproduct
can be a useful helper function
for the situation where you want to find all unique combinations of elements in
a number of different collections.
For example, consider a module that declares variables like the following:
If the goal is to create each of the defined subnets per each of the defined networks, creating the top-level networks can directly use var.networks
because it is already in a form where the resulting instances match one-to-one with map elements:
However, to declare all of the subnets with a single resource
block, you must first produce a collection whose elements represent all of the combinations of networks and subnets, so that each element itself represents a subnet:
The network_subnets
list in the example above creates one subnet instance per combination of network and subnet elements in the input variables. So for this example input:
The network_subnets
output would look similar to the following:
Related Functions
contains
tests whether a given list or set contains a given element value.flatten
is useful for flattening hierarchical data into a single list, for situations where the relationships between two object types are defined explicitly.setintersection
computes the intersection of multiple sets.setsubtract
computes the relative complement of two setssetunion
computes the union of multiple sets.