I'm trying to create a dynamic method to create vms in multiple environments that will be configured by the end user. Tried for loops with nested loops. flatten function, count, etc but haven't found a way to reach my goal. I have terrafrom.tfvars with the follwing structure:
Bigip_devices = {
main_hub = {
region = "eastus"
azs = ["1"] #Azure availabilty zones
vnet_name = "vnet-main" # Vnet name to deploy to
bigip_instance_count = 2 # Number of instnaces to delpoy
cluster = "yes" # Deploy as a cluster or stand alone device
version = "" # Leave blank for default value
sku = "" # Leave blank for default value - f5-bigip-virtual-edition-25m-best-hourly
offer = "" # Leave blank for default value - f5-big-ip-best
instance_type = "" # Leave blank for default value - Standard_DS3_v2
disable_password_authentication = "" #Leave blank for default value
tags = ""
}
spoke = {
region = "eastus"
azs = ["1","2"] #Azure availabilty zones
vnet_name = "vnet-spoke" # Vnet name to deploy to
bigip_instance_count = 4 # Number of instnaces to delpoy
cluster = "yes" # Deploy as a cluster or stand alone device
version = "" # Leave blank for default value
sku = "" # Leave blank for default value - f5-bigip-virtual-edition-25m-best-hourly
offer = "" # Leave blank for default value - f5-big-ip-best
instance_type = "" # Leave blank for default value - Standard_DS3_v2
disable_password_authentication = "" #Leave blank for default value
tags = ""
}
}
What is the correct method to iterate each key in the list( in the example the are 2 keys - main hub and spoke) and to create the amount of virtual machines corresponding to the bigip_instance_count setting. In the above example, I want to create 2 environments, one with 2 devices and the second with 4 devices. Is there a way to achieve it?
It would be really convenient if you transform the above complex JSON into a collection that has one element per resource you want to create. To achieve this, you could use the
flatten
function.With the above
flattened
function, you get below list of collection of resources, you would like to create.From the above collection, you could iterate & create resources with unique keys like below::
This way, the creation or updating of resources is guaranteed as each resource uses a unique key.
For more details, refer this discussion I had with Hashicorp personnel.