EventBridge scheduler schedules wrong ECS task definition

0

The AWS EventBridge Scheduler seemingly schedules the wrong ECS Task Definition. We noticed in the ECS that the tasks spun out via EventBridge is the wrong revision. Following log is from the CloudTrail:

...
"requestParameters": {
        "clientToken": "7929d6d1-048b-4f4a-afbd-2186b2a6b5a3",
        "count": 1,
        "launchType": "FARGATE",
        "networkConfiguration": {
            "awsvpcConfiguration": {
                "assignPublicIp": "DISABLED",
                "securityGroups": [
                    "sg-0334e4fe23d54ef07"
                ],
                "subnets": [
                    "subnet-007eb8e8a5c2a4cbb",
                    "subnet-0c8fcbcbe5c67b8f4"
                ]
            }
        },
        "cluster": "arn:aws:ecs:eu-west-1:xxxxxxxxx:cluster/prod",
        "enableExecuteCommand": false,
        "taskDefinition": "arn:aws:ecs:eu-west-1:xxxxxxxxxxx:task-definition/prod-scheduled-ar:31",
        "startedBy": "chronos-schedule/scheduled-ar",
        "enableECSManagedTags": false
    }
...

Does not correspond to the Terraform/Console configuration for the EventBridge Schedule:

TaskDefinitionArn:
"arn:aws:ecs:eu-west-1:xxxxxxxxxx:task-definition/prod-scheduled-ar:37",
TaskCount:
1,
LaunchType:
"FARGATE",
NetworkConfiguration:
{
awsvpcConfiguration:
{
Subnets:
["subnet-007eb8e8a5c2a4cbb","subnet-0c8fcbcbe5c67b8f4"],
SecurityGroups:
["sg-0334e4fe23d54ef07"],
AssignPublicIp:
"DISABLED"
}
},
PlatformVersion: --

Now we do update the EventBridge schedule every time we create the new revision but it doesn't RUN it when cron executes. I wonder why it is running the older task revisions. Any idea?

1 Answer
0

Hello,

It's better not to hardcode the task definition revision in the code, instead in event bridge scheduler resource provide the task definition family without the revision like this below example. So, whenever you create a new version of task definition it will automatically update the event bridge scheduler to run the latest revision of the task definition.

resource "aws_scheduler_schedule" "serverlessland-eb-ecs-invoke-schedule" {
      name = "serverlessland-eb-ecs-invoke-schedule"
      flexible_time_window {
      mode = "OFF"
  }
    schedule_expression = "rate(5 minute)"
    target {
      arn = aws_ecs_cluster.serverlessland-ecs-test-cluster.arn
      role_arn = aws_iam_role.serverlessland-eventbridge-invoke-ecs-role.arn
      ecs_parameters {
          task_count = 1
          task_definition_arn = aws_ecs_task_definition.serverlessland-ecs-task-definition.arn
          launch_type = "FARGATE"
    
          network_configuration {
                subnets          = aws_subnet.prod-subnet-public-1.*.id
                assign_public_ip = true
                security_groups = [aws_security_group.prod-sg.id]

   }
  }
  }
}

resource "aws_ecs_task_definition" "serverlessland-ecs-task-definition" {
  family                   = "serverlessland-ecs-task-definition"
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  cpu                      = 1024
  memory                   = 2048
  task_role_arn            = aws_iam_role.serverlessland-ecs-task-role.arn
  execution_role_arn       = aws_iam_role.serverlessland-ecs-task-execution-role.arn
  container_definitions    = <<TASK_DEFINITION
[
  {
    "name": "webcontainer",
    "image": "${local.container_image}",
    "cpu": 1024,
    "memory": 2048,
    "essential": true,

    "portMappings": [
        {
          "containerPort": 8080,
          "hostPort": 8080
        }
      ]
  }
]
TASK_DEFINITION
}
profile picture
answered 4 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions