Terraform v0.13.4
Faile terraform plan after adding new ECS service with Terraform modules
The first run works well:
main.tf
module "fargate" {
source = ecs"
services = {
api = {
task_definition = "api.json"
cpu = "256"
memory = "512"
}
}
}
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:
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:
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