How to use terraform setproduct function with more than 2 lists in the below scenario or is there any other way to achieve the end result.
In main.tf file I have the following data.
$ cat main.tf
variable "nodes" {
default = ["1", "2", "3"]
}
variable "ebs_volumes" {
default = [
{
ebs_name = "/dev/xdba"
ebs_size = "50"
ebs_type = "gp2"
},
{
ebs_name = "/dev/xdbb"
ebs_size = "20"
ebs_type = "gp2"
}
]
}
locals {
ebs_name = [for i in var.ebs_volumes : i.ebs_name]
ebs_size = [for i in var.ebs_volumes : i.ebs_size]
ebs_type = [for i in var.ebs_volumes : i.ebs_type]
}
locals {
node_disks = { for pair in setproduct(var.nodes, local.ebs_name) : "${pair[0]}:${pair[1]}" => {
node_index = pair[0]
ebs_name = pair[1]
ebs_size = [for i in var.ebs_volumes : i.ebs_size]
ebs_type = [for i in var.ebs_volumes : i.ebs_type]
} }
}
output "combined" {
value = local.node_disks
}
When I run terraform apply I get the following result
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
combined = {
"1:/dev/xdba" = {
"ebs_name" = "/dev/xdba"
"ebs_size" = [
"50",
"20",
]
"ebs_type" = [
"gp2",
"gp2",
]
"node_index" = "1"
}
"1:/dev/xdbb" = {
"ebs_name" = "/dev/xdbb"
"ebs_size" = [
"50",
"20",
]
"ebs_type" = [
"gp2",
"gp2",
]
"node_index" = "1"
}
"2:/dev/xdba" = {
"ebs_name" = "/dev/xdba"
"ebs_size" = [
"50",
"20",
]
"ebs_type" = [
"gp2",
"gp2",
]
"node_index" = "2"
}
"2:/dev/xdbb" = {
"ebs_name" = "/dev/xdbb"
"ebs_size" = [
"50",
"20",
]
"ebs_type" = [
"gp2",
"gp2",
]
"node_index" = "2"
}
"3:/dev/xdba" = {
"ebs_name" = "/dev/xdba"
"ebs_size" = [
"50",
"20",
]
"ebs_type" = [
"gp2",
"gp2",
]
"node_index" = "3"
}
"3:/dev/xdbb" = {
"ebs_name" = "/dev/xdbb"
"ebs_size" = [
"50",
"20",
]
"ebs_type" = [
"gp2",
"gp2",
]
"node_index" = "3"
}
}
Instead I want to be able to have the result as shown below (psudo code):
combined = {
"1:/dev/xvdb" = {
"disk_dev_path" = "/dev/xvdb"
"node_name" = "1"
"disk_size" = "10"
"disk_type" = "gp2"
}
"1:/dev/xvdc" = {
"disk_dev_path" = "/dev/xvdc"
"node_name" = "1"
"disk_size" = "30"
"disk_type" = "gp2"
}
"2:/dev/xvdb" = {
"disk_dev_path" = "/dev/xvdb"
"node_name" = "2"
"disk_size" = "10"
"disk_type" = "gp2"
}
"2:/dev/xvdc" = {
"disk_dev_path" = "/dev/xvdc"
"node_name" = "2"
"disk_size" = "30"
"disk_type" = "gp2"
}
"3:/dev/xvdb" = {
"disk_dev_path" = "/dev/xvdb"
"node_name" = "3"
"disk_size" = "10"
"disk_type" = "gp2"
}
"3:/dev/xvdc" = {
"disk_dev_path" = "/dev/xvdc"
"node_name" = "3"
"disk_size" = "30"
"disk_type" = "gp2"
}
}
How to achieve this?
I need this in order to use this in for_each to create ec2 instances and ebs volumes and attach them respectively whenever user adds an input in the variable using our ec2 module.