I have an enclave with X servers, each with a different replicaset count per se and different memory , cpu, core, etc. settings. I want to setup a map(objects) to define all these servers and then down in my compute.tf have a for_each, for or count variable that keys off the replicaset count in the map(objects). Is this doable?
For example, in an enclave.tf file I have:
locals {
enclaves = {
"artifactory" = {
vm_count = 2
vm_name = "art"
}
"gitlab" = {
vm_count = 1
vm_name = "gls"
}
}
hosts = flatten([
for host, cfg in local.enclaves : [
for id in range(cfg.vm_count) : {
host = host
count = id + 1
}
]
])
}
And in another compute.tf file, I have the an aws_instance resource definition that I want to iterate over the above “hosts”, using the counter value in each. As in:
[...snip...]
resource "aws_instance" "enclave" {
I want a for_each or count or some kind of for loop here to loop through the configuration above, making use of the 'vm_count'.
[...snip...]
}
Does this code above make logical sense and first of all, is it physically doable in this manner?
Using terraform console, typing ‘local.hosts’ shows me I am iterating over the hosts correctly, but what doesn’t seem to pan out is the for_each or count or for setting down in the aws_instance definition.
Has anyone have a better means to accomplish this style of enclave definition?
I think for any more suggestion you would have to explain what you perceive as wrong with your current configuration. What do you mean by “doesn’t seem to pan out is the for_each or count or for setting down in the aws_instance definition.”?
Using a collection of objects with for_each is a standard way to create multiple instances of a particular resource type, and from the limited example here what you have doesn’t look particularly complicated.
Yeah, it’s not all that complicated however I am unable to pull the vm_count and have it be used in a loop for the aws_instance resources. I want the vm_count to depict how many replicas (think HA or clustering) to create without having to create each individual definition up in the map of objects. I have this working if I have multiple servers defined say for a postgres cluster, using a postgres1, postgres2, postgres3, … but I want to make use of the vm_count instead of each individual postgresX entry in my map of objects.
Does this clarify?
Yes, my limited example code above doesn’t give the overall picture, but I thought it would give enough to convey my desired configuration.
So the question is really about having to use for id in range(cfg.vm_count) to create multiple objects? You could move that around into different expressions, but you still need to create the config that number of times, so the iteration must exist somewhere. Given the example data, I would say what you have is fine.
But getting the resulting count to be used in the aws_instance resource’s for_each or count or for looping is the issue. I can feel it being really close to working, but it won’t.
How would I get “count = 2” to be pulled out of local.hosts and used as a count iteration in aws_instance?
You already “pulled that out” by creating 2 entires in local.hosts, you don’t need to do anything else. Generate the objects corresponding to each instance, then use for_each to apply those to each instance’s configuration.