snippetterraformMinor
How to produce a map from the cartesian product of two lists to use with for_each on a resource with Terraform
Viewed 0 times
maptheproducefor_eachresourcewithproductliststwocartesian
Problem
I need to create a datadog synthetics monitors with the resource
I need to monitor multiples cluster, with multiple point of view :
There is a function
I would need to produce a map which look like this :
How could I do that ?
datadog_synthetics_testI need to monitor multiples cluster, with multiple point of view :
# Clusters
variable "datadog_gke_clusters" {
default = [
# NPRD
{
name = "cluster01"
environment = "nprd"
url = {
private = "https://private.domain-priv.com"
public = "https://public.domain.com"
}
},
# PROD
{
name = "cluster02"
environment = "nprd"
url = {
private = "https://private.domain-priv.com"
public = "https://public.domain.com"
}
}
]
}
# Point of views
variable "gke_slo" {
description = "Response time threshold in ms to respond to a request."
default = [
{
zone = private
monitor_threshold = 50
},
{
zone = public
monitor_threshold = 100
},
]
}for_each need a map or a list of stringsresource "datadog_synthetics_test" "gke-monitoring" {
for_each = ... # cartesian product of lists
type = "api"
subtype = "http"
....
}There is a function
[setproduct(sets...)][1] which produce a list which combine values of each list, but not a map.I would need to produce a map which look like this :
{
"cluster01-private" = {
name = "cluster01"
environment = "nprd"
url = {
private = "https://private.cluster01.domain-priv.com"
public = "https://public.cluster01.domain.com"
}
zone = private
monitor_threshold = 50
},
"cluster01-public" = {
name = "cluster01"
environment = "nprd"
url = {
private = "https://private.cluster01.domain-priv.com"
public = "https://public.cluster01.domain.com"
}
zone = public
monitor_threshold = 100
},
# PROD
.... etc
}How could I do that ?
Solution
In 2 steps, you can first create your cartesian product in a variable:
And then use this new variable in your resource block:
Have a good day!
locals {
datadog_gke_clusters_by_pov = flatten([
for cluster in var.datadog_gke_clusters : [
for pov in var.gke_slo_ : {
cluster = cluster
pov = pov
}
]
])
}And then use this new variable in your resource block:
resource "datadog_synthetics_test" "gke-monitoring" {
for_each = {
for valuue in local.datadog_gke_clusters_by_pov : "${valuue.cluster.name}-${valuue.pov.zone_name}" => valuue
}
...
# here you can use each.value.cluster.* and each.value.pov.*
}Have a good day!
Code Snippets
locals {
datadog_gke_clusters_by_pov = flatten([
for cluster in var.datadog_gke_clusters : [
for pov in var.gke_slo_ : {
cluster = cluster
pov = pov
}
]
])
}resource "datadog_synthetics_test" "gke-monitoring" {
for_each = {
for valuue in local.datadog_gke_clusters_by_pov : "${valuue.cluster.name}-${valuue.pov.zone_name}" => valuue
}
...
# here you can use each.value.cluster.* and each.value.pov.*
}Context
StackExchange DevOps Q#12060, answer score: 3
Revisions (0)
No revisions yet.