`I have a local variable that looks like the below in the outputs
+ karpenter_node_instance_types = [
+ [
+ "t3.small",
+ "t3.medium",
+ "t3.xlarge",
],
+ [
+ "g5.xlarge",
+ "p2.xlarge",
],
]
I am trying to break in this into a simple list of object so that I can use each one of them in my data block.
I want something like :
+ karpenter_instance_list = [
+ {
+ name = "g5.xlarge"
},
+ {
+ name = "p2.xlarge"
},
+ {
+ name = "t3.xlarge"
},
+ {
+ name = "t3.medium"
},
+ {
+ name = "t3.small"
},
]
I can then simply loop thru this and use it in the data block as:
data "aws_ec2_instance_type" "find_karpenter_instance_type" {
for_each = { for index, instance in local.karpenter_instance_list : index => instance if(length(instance.name) > 0)}
instance_type = each.value.name
}
Any ideas on how I can generate karpenter_instance_list ?? As of now I can only get either the values from index --> 0 or index 1, there could be values more than what I have in the list.
I am doing this as of now to get values from index 0 and 1
karpenter_instance_list_one = flatten(toset([
for index, instance_type in local.karpenter_node_instance_types : {
name = element(instance_type, index)
]))
karpenter_instance_list_two = flatten(toset([
for index, instance_type in local.karpenter_node_instance_types : {
name = element(instance_type, index + 1)
}
]))
karpenter_instance_list = distinct(setunion(local.karpenter_instance_list_one, local.karpenter_instance_list_two))
You may use the flatten
function to convert the list of lists into a list, then iterating it with a for returning a list of objects, as follows:
locals {
karpenter_node_instance_types = [
[
"t3.small",
"t3.medium",
"t3.xlarge",
],
[
"g5.xlarge",
"p2.xlarge",
],
]
karpenter_instance_list = [
for instance_type in flatten(local.karpenter_node_instance_types) : {
name = instance_type
}
]
}