# Complex count setup

**URL:** <https://discuss.hashicorp.com/t/complex-count-setup/73499>\
**Category:** Terraform\
**Created:** [February 26, 2025, 11:17pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499 "2025-02-26T23:17:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregg.a.lowrimore](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gregg.a.lowrimore/32/44944_2.png) [@gregg.a.lowrimore](https://discuss.hashicorp.com/u/gregg.a.lowrimore)\
**Post date:** [February 26, 2025, 11:17pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/1 "2025-02-26T23:17:48Z")

</div>

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:

```auto
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:

```auto
[...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?

---

<div class="post-metadata">

**Author:** ![jbardin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jbardin/32/3325_2.png) [@jbardin](https://discuss.hashicorp.com/u/jbardin)\
**Post date:** [February 27, 2025, 3:13pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/2 "2025-02-27T15:13:12Z")

</div>

Hi @gregg.a.lowrimore,

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.

---

<div class="post-metadata">

**Author:** ![gregg.a.lowrimore](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gregg.a.lowrimore/32/44944_2.png) [@gregg.a.lowrimore](https://discuss.hashicorp.com/u/gregg.a.lowrimore)\
**Post date:** [February 27, 2025, 3:49pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/3 "2025-02-27T15:49:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbardin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jbardin/32/3325_2.png) [@jbardin](https://discuss.hashicorp.com/u/jbardin)\
**Post date:** [February 27, 2025, 6:06pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/4 "2025-02-27T18:06:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gregg.a.lowrimore](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gregg.a.lowrimore/32/44944_2.png) [@gregg.a.lowrimore](https://discuss.hashicorp.com/u/gregg.a.lowrimore)\
**Post date:** [February 27, 2025, 6:14pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/5 "2025-02-27T18:14:08Z")

</div>

Yeah, using the terraform console, I can print out the local.hosts from my example, to get

```auto
> local.hosts
[
  {
    "count" = 1
    "host" = "artifactory"
  },
  {
    "count" = 2
    "host" = "artifactory"
  },
  {
    "count" = 1
    "host" = "gitlab"
  },
]

```

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?

---

<div class="post-metadata">

**Author:** ![gregg.a.lowrimore](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gregg.a.lowrimore/32/44944_2.png) [@gregg.a.lowrimore](https://discuss.hashicorp.com/u/gregg.a.lowrimore)\
**Post date:** [February 27, 2025, 6:15pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/6 "2025-02-27T18:15:03Z")

</div>

Maybe I’m just making this too difficult and just need to keep it simpler. I dunno.

---

<div class="post-metadata">

**Author:** ![jbardin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jbardin/32/3325_2.png) [@jbardin](https://discuss.hashicorp.com/u/jbardin)\
**Post date:** [February 27, 2025, 7:57pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/7 "2025-02-27T19:57:37Z")

</div>

> [@gregg.a.lowrimore](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![gregg.a.lowrimore](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gregg.a.lowrimore/32/44944_2.png) [@gregg.a.lowrimore](https://discuss.hashicorp.com/u/gregg.a.lowrimore)\
**Post date:** [February 27, 2025, 8:53pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/8 "2025-02-27T20:53:55Z")

</div>

So Gen.AI to the rescue…

```auto
# Define a map of objects
locals {
  instances = {
    "instance1" = {
      ami = "ami-abc123"
      instance_type = "t2.micro"
      vpc_security_group_ids = ["sg-123456"]
      count = 2
    },
    "instance2" = {
      ami = "ami-def456"
      instance_type = "t2.small"
      vpc_security_group_ids = ["sg-789012"]
      count = 3
    },
    "instance3" = {
      ami = "ami-ghi789"
      instance_type = "t2.medium"
      vpc_security_group_ids = ["sg-345678"]
      count = 1
    }
  }
}

# Create a list of objects with counts expanded
locals {
  instance_list = flatten([
    for key, value in local.instances : [
      for i in range(value.count) : merge(value, { name = "${key}-${i+1}" })
    ]
  ])
}

```

Then for the aws\_instance resource looping over the count from above:

```auto
resource "aws_instance" "example" {
  for_each = { for idx, value in local.instance_list : idx => value }

 [...snip...]
}

```

---

<div class="post-metadata">

**Author:** ![gregg.a.lowrimore](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gregg.a.lowrimore/32/44944_2.png) [@gregg.a.lowrimore](https://discuss.hashicorp.com/u/gregg.a.lowrimore)\
**Post date:** [February 27, 2025, 8:54pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/9 "2025-02-27T20:54:43Z")

</div>

Now to go implement this …

---

<div class="post-metadata">

**Author:** ![gregg.a.lowrimore](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/gregg.a.lowrimore/32/44944_2.png) [@gregg.a.lowrimore](https://discuss.hashicorp.com/u/gregg.a.lowrimore)\
**Post date:** [February 28, 2025, 5:16pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/10 "2025-02-28T17:16:36Z")

</div>

Made a slight modification but this worked like a charm. Super happy.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/hashicorp/original/1X/79bf76adfd92a75631996933098f9ce29d37abf3.svg) [@system](https://discuss.hashicorp.com/u/system)\
**Post date:** [August 27, 2025, 5:17pm UTC](https://discuss.hashicorp.com/t/complex-count-setup/73499/11 "2025-08-27T17:17:36Z")

</div>

Closed on August 27, 2025, 5:17pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
