# Taint a resource created with for\_each

**URL:** <https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889>\
**Category:** Terraform\
**Created:** [September 18, 2019, 7:47pm UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889 "2019-09-18T19:47:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriskinsman](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chriskinsman/32/2290_2.png) [@chriskinsman](https://discuss.hashicorp.com/u/chriskinsman)\
**Post date:** [September 18, 2019, 7:47pm UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889/1 "2019-09-18T19:47:21Z")

</div>

I am creating a number of ECS services like this:

```
resource "aws_ecs_service" "service" {
  for_each = toset(var.cluster_names)

  name = "${var.repository}_${var.group}_${var.service_name}"
  cluster = "${each.key}"
  desired_count = "${var.desired_count}"

  ordered_placement_strategy {
    type = "spread"
    field = "instanceId"
  }

  ordered_placement_strategy {
    type = "spread"
    field = "attribute:ecs.availability-zone"
  }

  load_balancer {
    container_name = "${var.service_name}"
    container_port = "${var.port}"
    target_group_arn = "${var.target_group_arn}"
  }

  # Track the latest ACTIVE revision
  task_definition = "${aws_ecs_task_definition.task_def.family}:${max("${aws_ecs_task_definition.task_def.revision}", "${data.aws_ecs_task_definition.task_def.revision}")}"

}

```

How do I taint the resources created this way?

The following don’t work  
terraform taint aws\_ece\_service.service  
terraform taint aws\_ece\_service.service.\*  
terraform taint aws\_ece\_service.service[_]  
terraform taint aws\_ece\_service.service_

I believe the splats only work with lists and for\_each returns a map. Not sure how to reference elements in the map…

---

<div class="post-metadata">

**Author:** ![outthought](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/outthought/32/2170_2.png) [@outthought](https://discuss.hashicorp.com/u/outthought)\
**Post date:** [September 18, 2019, 11:17pm UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889/2 "2019-09-18T23:17:08Z")

</div>

Try for example,

```
aws_ece_service.service["a_cluster"]

```

To refer to one of the resource instances associated with a resource block the syntax is

```
<TYPE>.<NAME>[<KEY>]

```

> **[Resources - Configuration Language - Terraform by HashiCorp](https://www.terraform.io/docs/configuration/resources.html#for_each-multiple-resource-instances-defined-by-a-map-or-set-of-strings)**
>
> Resources are the most important element in a Terraform configuration. Each resource corresponds to an infrastructure object, such as a virtual network or compute instance.

---

<div class="post-metadata">

**Author:** ![chriskinsman](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chriskinsman/32/2290_2.png) [@chriskinsman](https://discuss.hashicorp.com/u/chriskinsman)\
**Post date:** [September 19, 2019, 1:01am UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889/3 "2019-09-19T01:01:46Z")

</div>

Seemed so promising but no go:

```
terraform taint module.servotron.aws_ecs_service.service["FrontEnd-OnDemand"]

```

Yields

```
Error: Index value required

```

on line 1:  
(source code not available)

Index brackets must contain either a literal number or a literal string.

---

<div class="post-metadata">

**Author:** ![amy-hashi](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/amy-hashi/32/1369_2.png) [@amy-hashi](https://discuss.hashicorp.com/u/amy-hashi)\
**Post date:** [September 19, 2019, 1:15am UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889/4 "2019-09-19T01:15:06Z")

</div>

Run ‘terraform state list’ to see how each resource is identified by terraform.

---

<div class="post-metadata">

**Author:** ![chriskinsman](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chriskinsman/32/2290_2.png) [@chriskinsman](https://discuss.hashicorp.com/u/chriskinsman)\
**Post date:** [September 19, 2019, 1:56pm UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889/5 "2019-09-19T13:56:22Z")

</div>

Awesome. That indeed shows that I was using the right syntax. taint must just be broken for this scenario.

I will log a bug against terraform.

---

<div class="post-metadata">

**Author:** ![scott1138](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/scott1138/32/2489_2.png) [@scott1138](https://discuss.hashicorp.com/u/scott1138)\
**Post date:** [October 24, 2019, 7:36pm UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889/6 "2019-10-24T19:36:31Z")

</div>

Did you get anywhere with this issue?

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [October 24, 2019, 8:51pm UTC](https://discuss.hashicorp.com/t/taint-a-resource-created-with-for-each/2889/7 "2019-10-24T20:51:04Z")

</div>

Hi @outthought!

That is indeed the correct index syntax for a `for_each` resource instance. A gotcha with it is that quotes are often interpreted by your shell, so some form of escaping would be needed.

In a Unix-style shell we can use single quotes around the whole address to mark the quotes as literal:

```auto
terraform taint 'aws_ece_service.service["a_cluster"]'

```

On Windows systems, single quotes are not a conventional way to mark literals so we must use backslash escapes instead:

```auto
terraform taint aws_ece_service.service[\"a_cluster\"]

```

Without that escaping, the value will often be received by Terraform with some sort of corruption, like `aws_ece_service.service[a_cluster]`, which then leads to a parse error in Terraform.
