Hi All,
I couldn’t figure out how I can loop through the sum of values of the below map.
variable "images" {
default = {
"rhel-8-factory-os-ready" = {
"availability_zone" = "eu-fra-1ah"
"flavor" = 4
"instance_count" = 2
"image_name" = "rhel-8-factory-os-ready"
},
"rhel-7-factory-os-ready" = {
"availability_zone" = "eu-fra-1ai"
"instance_count" = 3
"flavor" = 3
"image_name" = "rhel-7-factory-os-ready"
},
"rhel-6-factory-os-ready" = {
"availability_zone" = "eu-fra-1ah"
"instance_count" = 3
"flavor" = 3
"image_name" = "rhel-6-factory-os-ready"
}
}
}
Here, I’ve to iterate through the sum of instance_count
attribute of the all the keys & create instances based on the instance_count
.
I could calculate the sum of instance_count
, with below inbuilt functions.
locals {
list_sum = length(flatten([for i in var.images: range(i["instance_count"])]))
}
How can I iterate through the list_sum
variable & create the resources based on instance_count
?
I created below lists to create resources ::
locals {
list_images = tolist(keys(var.images))
list_instance_count = [for i in var.images: i["instance_count"]]
list_flavors = [for i in var.images: i["flavor"]]
list_image_names = [for i in var.images: i["image_name"]]
list_availability_zones = [for i in var.images: i["availability_zone"]]
}
My resource ::
resource "openstack_compute_instance_v2" "instance" {
count = local.list_sum
image_name = element(local.list_image_names, count.index +1 )
flavor_id = element(local.list_flavors, (count.index + 1) )
name = element(local.list_image_names, (count.index + 1) )
security_groups = var.security_group
availability_zone = element(local.list_availability_zones, (count.index + 1) )
key_pair = "foptst"
network {
name = var.network_name
}
}
By now, you may be knowing that my iteration is incorrect. My resource block has to create the number of resources based on instance_count
var ie., 2 instances of rhel-8-factory-os-ready
, 3 instances of rhel-7-factory-os-ready
and 3 instances of rhel-6-factory-os-ready
.
Because of incorrect looping, I couldn’t get it. It would be great if someone could help me how to iterate properly to create resources as expected.
Many Thanks in advance,
Harsha