# How to loop on index

**URL:** <https://discuss.hashicorp.com/t/how-to-loop-on-index/50607>\
**Category:** Terraform\
**Created:** [February 23, 2023, 5:51pm UTC](https://discuss.hashicorp.com/t/how-to-loop-on-index/50607 "2023-02-23T17:51:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yanikku95](https://avatars.discourse-cdn.com/v4/letter/y/5f8ce5/32.png) [@yanikku95](https://discuss.hashicorp.com/u/yanikku95)\
**Post date:** [February 23, 2023, 5:51pm UTC](https://discuss.hashicorp.com/t/how-to-loop-on-index/50607/1 "2023-02-23T17:51:45Z")

</div>

Hello everybody,

I’m a newbie and I need some help.

I want to loop on index to create or not some ecs resources on aws.

My module like this :

```auto
resource "aws_ecs_service" "this" {
  for_each = var.ecs_cluster_definitions.ecs_service
  name	= each.value.name
  cluster	= aws_ecs_cluster.ecs_cluster.arn
  platform_version = each.value.platform_version
  task_definition = each.value.task_definition
  ...
}

```

My main.tf on the principal code like this :

```auto
module "ecs" {
  source = " ********"
  for_each = {
   for ecs_cluster_configuration in local.ecs_cluster_configuration:
    ecs_cluster_configuration.ecs_cluster_definition.ecs_cluster_name => 
    ecs_cluster_configuration
   }
   ecs_cluster_definitions = each.value.ecs_cluster_definition
  ....

```

Remark : ecs\_cluster\_definitions is a variable with type any

My local.tf like this :

```auto
ecs_cluster_configuration = [
    {
      ecs_cluster_definition = {
        ecs_cluster_name = "ecs-cluster-${local.nameattr3}"
        ecs_cluster_description = "ECS ${var.apps} ${var.Environment} ${var.Sub_Environment} 
       ${var.Domain}"
	  ecs_service = {
            "service1" = {
              name = "ecs-container-${var.Sub_Environment}-${var.apps}- 
              service1-${var.Domain}"
              platform_version = "LATEST"
              ....
             },
              "service2" = {
               ....
             },
              "service3" = {
               ....
             },
         }
    }
  }
]

```

My goal is to not create service2 for example if var.create\_service value is false but create service1 and service3 if var.create\_service is true.

If I understand “service1”, “service2” and “service3” are index. So I want to loop on this index to create or not the service

I test some code but it’s not resolve my issue.

Can you help me please ?

Thanks

---

<div class="post-metadata">

**Author:** ![yanikku95](https://avatars.discourse-cdn.com/v4/letter/y/5f8ce5/32.png) [@yanikku95](https://discuss.hashicorp.com/u/yanikku95)\
**Post date:** [February 27, 2023, 8:59am UTC](https://discuss.hashicorp.com/t/how-to-loop-on-index/50607/2 "2023-02-27T08:59:18Z")

</div>

Hi,

Do you think is it possible to have a condition to create or not resource “service2” ?

Thanks for your help

---

<div class="post-metadata">

**Author:** ![satyawan.gaje](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@satyawan.gaje](https://discuss.hashicorp.com/u/satyawan.gaje)\
**Post date:** [December 4, 2023, 7:35am UTC](https://discuss.hashicorp.com/t/how-to-loop-on-index/50607/3 "2023-12-04T07:35:39Z")

</div>

use the “count” property in resource and set the condition like “true ? 1:0” modify the conditions according to required logic.  
\*\*when count=0 resource will be skiped.
