# Data source Refreshing state fail Invalid index

**URL:** <https://discuss.hashicorp.com/t/data-source-refreshing-state-fail-invalid-index/15312>\
**Category:** Terraform\
**Created:** [October 8, 2020, 4:31pm UTC](https://discuss.hashicorp.com/t/data-source-refreshing-state-fail-invalid-index/15312 "2020-10-08T16:31:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oleksandr-hyuna](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/oleksandr-hyuna/32/12573_2.png) [@oleksandr-hyuna](https://discuss.hashicorp.com/u/oleksandr-hyuna)\
**Post date:** [October 8, 2020, 4:31pm UTC](https://discuss.hashicorp.com/t/data-source-refreshing-state-fail-invalid-index/15312/1 "2020-10-08T16:31:17Z")

</div>

Terraform v0.13.4  
Faile terraform plan after adding new ECS service with Terraform modules  
The first run works well:

```auto
main.tf

module "fargate" {
  source = ecs"
  services = {
    api = {
      task_definition = "api.json"
      cpu = "256"
      memory = "512"
    }
  }
}

```

```auto
ecs/main.tf

data "template_file" "tasks" {
  for_each = var.services

  template = file("${path.cwd}/${lookup(each.value, "task_definition", "api.json")}")

  vars = {
    env = terraform.workspace
  }
}

resource "aws_ecs_task_definition" "this" {
  for_each = var.services

  family = "${terraform.workspace}-${each.key}"
  container_definitions = data.template_file.tasks[each.key].rendered
  requires_compatibilities = ["FARGATE"]
  network_mode = "awsvpc"
  cpu = lookup(each.value, "cpu", "256")
  memory = lookup(each.value, "memory", "512")

  tags = {
    Name = "${var.name}-${terraform.workspace}-${each.key}-task-definition"
    environment = terraform.workspace
  }
}

ecs/variables.tf

variable "services" {}

```

Everything created well, but if add one more service:

```auto
main.tf

module "fargate" {
  source = ecs"
  services = {
    api = {
      task_definition = "api.json"
      cpu = "256"
      memory = "512"
    }
    api2 = {
      task_definition = "api.json"
      cpu = "256"
      memory = "512"
    }
  }
}

```

i run terraform plan and get:

```auto
Error: Invalid index

  on ../../../terraform/aws-ecs-fargate/main.tf line 22, in resource "aws_ecs_task_definition" "this":
 22: container_definitions = data.template_file.tasks[each.key].rendered
    |----------------
    | data.template_file.tasks is object with 2 attributes
    | each.key is "api2"

The given key does not identify an element in this collection value.

```

But if u run remove state and run terraform plan all works ok, on previous terraform version 0.12.~ I have no such trouble with state refresh.  
Please help
