2019-01-15 18:47:14 +01:00
---
2021-11-23 00:57:25 +01:00
layout: "language"
page_title: "setproduct - Functions - Configuration Language"
sidebar_current: "docs-funcs-collection-setproduct"
2019-01-15 18:47:14 +01:00
description: |-
The setproduct function finds all of the possible combinations of elements
from all of the given sets by computing the cartesian product.
---
# `setproduct` Function
The `setproduct` function finds all of the possible combinations of elements
from all of the given sets by computing the
2019-09-05 19:08:34 +02:00
[Cartesian product ](https://en.wikipedia.org/wiki/Cartesian_product ).
2019-01-15 18:47:14 +01:00
```hcl
setproduct(sets...)
```
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.
```
> setproduct(["development", "staging", "production"], ["app1", "app2"])
[
[
"development",
"app1",
],
[
"development",
"app2",
],
[
"staging",
"app1",
],
[
"staging",
"app2",
],
[
"production",
"app1",
],
[
"production",
"app2",
],
]
```
2020-03-23 13:55:08 +01:00
You must pass at least two arguments to this function.
2019-01-15 18:47:14 +01:00
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:
```
> setproduct(["development", "staging", "production"], [])
[]
```
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:
```
> setproduct(["a"], ["b"])
[
[
"a",
"b",
],
]
```
Each argument must have a consistent type for all of its elements. If not,
Terraform 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:
```
> setproduct(["staging", "production"], ["a", 2])
[
[
"staging",
"a",
],
[
"staging",
"2",
],
[
"production",
"a",
],
[
"production",
"2",
],
]
```
2019-01-15 20:51:21 +01:00
2019-10-11 21:02:24 +02:00
## Finding combinations for `for_each`
The
2021-11-23 00:57:25 +01:00
[resource `for_each` ](/docs/language/meta-arguments/for_each.html )
2019-10-11 21:02:24 +02:00
and
2021-11-23 00:57:25 +01:00
[`dynamic` block ](/docs/language/expressions/dynamic-blocks.html )
2019-10-11 21:02:24 +02:00
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:
```hcl
variable "networks" {
type = map(object({
base_cidr_block = string
}))
}
variable "subnets" {
type = map(object({
number = number
}))
}
```
2021-11-05 15:51:59 +01:00
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:
2019-10-11 21:02:24 +02:00
```hcl
resource "aws_vpc" "example" {
for_each = var.networks
cidr_block = each.value.base_cidr_block
}
```
2021-11-05 15:51:59 +01:00
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:
2019-10-11 21:02:24 +02:00
```hcl
locals {
2021-11-05 15:51:59 +01:00
# setproduct works with sets and lists, but the variables are both maps
# so convert them first.
2019-10-11 21:02:24 +02:00
networks = [
for key, network in var.networks : {
key = key
cidr_block = network.cidr_block
}
]
subnets = [
for key, subnet in var.subnets : {
key = key
number = subnet.number
}
]
network_subnets = [
# in pair, element zero is a network and element one is a subnet,
# in all unique combinations.
for pair in setproduct(local.networks, local.subnets) : {
network_key = pair[0].key
subnet_key = pair[1].key
network_id = aws_vpc.example[pair[0].key].id
2021-11-05 15:51:59 +01:00
# The cidr_block is derived from the corresponding network. Refer to the
2019-10-11 21:02:24 +02:00
# cidrsubnet function for more information on how this calculation works.
cidr_block = cidrsubnet(pair[0].cidr_block, 4, pair[1].number)
}
]
}
resource "aws_subnet" "example" {
2021-11-05 15:51:59 +01:00
# local.network_subnets is a list, so project it into a map
# where each key is unique. Combine the network and subnet keys to
2019-10-11 21:02:24 +02:00
# produce a single unique key per instance.
for_each = {
for subnet in local.network_subnets : "${subnet.network_key}.${subnet.subnet_key}" => subnet
}
vpc_id = each.value.network_id
availability_zone = each.value.subnet_key
2020-12-08 15:05:37 +01:00
cidr_block = each.value.cidr_block
2019-10-11 21:02:24 +02:00
}
```
2021-11-15 16:49:37 +01:00
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:
2021-11-05 15:51:59 +01:00
2021-11-15 16:49:37 +01:00
```hcl
2021-11-05 15:51:59 +01:00
networks = {
a = {
base_cidr_block = "10.1.0.0/16"
}
b = {
base_cidr_block = "10.2.0.0/16"
}
}
subnets = {
a = {
number = 1
}
b = {
number = 2
}
c = {
number = 3
}
}
```
2021-11-15 16:49:37 +01:00
The `nework_subnets` output would look similar to the following:
```hcl
[
{
"cidr_block" = "10.1.16.0/20"
"network_id" = "vpc-0bfb00ca6173ea5aa"
"network_key" = "a"
"subnet_key" = "a"
},
{
"cidr_block" = "10.1.32.0/20"
"network_id" = "vpc-0bfb00ca6173ea5aa"
"network_key" = "a"
"subnet_key" = "b"
},
{
"cidr_block" = "10.1.48.0/20"
"network_id" = "vpc-0bfb00ca6173ea5aa"
"network_key" = "a"
"subnet_key" = "c"
},
{
"cidr_block" = "10.2.16.0/20"
"network_id" = "vpc-0d193e011f6211a7d"
"network_key" = "b"
"subnet_key" = "a"
},
{
"cidr_block" = "10.2.32.0/20"
"network_id" = "vpc-0d193e011f6211a7d"
"network_key" = "b"
"subnet_key" = "b"
},
{
"cidr_block" = "10.2.48.0/20"
"network_id" = "vpc-0d193e011f6211a7d"
"network_key" = "b"
"subnet_key" = "c"
},
2021-11-05 15:51:59 +01:00
]
```
2019-10-11 21:02:24 +02:00
2019-01-15 20:51:21 +01:00
## Related Functions
2021-11-23 00:57:25 +01:00
- [`contains` ](./contains.html ) tests whether a given list or set contains
2019-01-15 20:51:21 +01:00
a given element value.
2021-11-23 00:57:25 +01:00
- [`flatten` ](./flatten.html ) is useful for flattening hierarchical data
2019-10-11 21:02:24 +02:00
into a single list, for situations where the relationships between two
object types are defined explicitly.
2021-11-23 00:57:25 +01:00
- [`setintersection` ](./setintersection.html ) computes the _intersection_ of
2019-01-15 20:51:21 +01:00
multiple sets.
2021-11-23 00:57:25 +01:00
- [`setsubtract` ](./setsubtract.html ) computes the _relative complement_ of two sets
- [`setunion` ](./setunion.html ) computes the _union_ of multiple
2019-01-15 20:51:21 +01:00
sets.