EDIT:My Terraform looks like that:
resource "aws_ecs_task_definition" "task_definition" {family = "${var.application_name}"container_definitions = "${data.template_file.container_definition_tpl.rendered}"requires_compatibilities = ["FARGATE"]network_mode = "awsvpc"execution_role_arn = "arn:aws:iam::${var.account_id}:role/app_execution_role"task_role_arn = "arn:aws:iam::${var.account_id}:role/app_task_role"cpu = 256memory = 512}resource "aws_cloudwatch_event_rule" "scheduled_task" {name = "${var.name}_${var.environment}_scheduled_task"description = "Run ${var.name}_${var.environment} task at a scheduled time rate (1 day)"schedule_expression = "cron(* 9 * ? *)"}resource "aws_cloudwatch_event_target" "scheduled_task" {target_id = "${var.name}_${var.environment}_scheduled_task_target"rule = "${aws_cloudwatch_event_rule.scheduled_task.name}"arn = "${data.aws_ecs_cluster.ecs_cluster.id}"role_arn = "${aws_iam_role.ecs_events.arn}"ecs_target {task_count = 1task_definition_arn = "${aws_ecs_task_definition.task_definition.arn}"launch_type="FARGATE"platform_version="LATEST"network_configuration {assign_public_ip = falsesecurity_groups= [data.aws_security_group.ecs_security_group.id]subnets =data.aws_subnet_ids.private_subnet_ids.ids}}
I have a scheduled Task which runs once a day. But it creates a new Task and the other task is still RUNNUNG, so i would have 7 task after one week.
In the end the solution was very simple.
Problem:
The task ran in an ECS-Serivce and was also called as scheduled-event. This caused a lot of problems, because the task ran twice at given times.
Solution:
In the end, the solution was to remove the ECS service because the job did not meet the requirements of an ECS service. The answer was to call the task only via the Schedules task. In addition, the implementation had to shut down the container, which ensured that when the task was completed, the task would close automatically and no unnecessary tasks would be there.