Skip to content

tsukiy0's blog

Update ECS scheduled task

May 20, 2021

Scheduled ECS tasks refer to a specific task definition version. Typically after deploying a new task definition, we need to update these scheduled task to use the deployed version.

The script requires:

  • awscli
  • jq
  • Environment
    • TASKDEF_NAME is the name of the task definition
    • SCHEDULED_TASK_NAME is the name fo the scheduled task
    • WORKSPACE path to save updated scheduled task JSON
TASKDEF_JSON=$(aws ecs describe-task-definition --task-definition ${TASKDEF_NAME}
SCHEDULED_TASK_JSON=$(aws events list-targets-by-rule --rule ${SCHEDULED_TASK_NAME}
TASKDEF_ARN=$(echo ${TASKDEF_JSON} | jq -r .taskDefinition.taskDefinitionArn)
echo ${SCHEDULED_TASK_JSON} | jq --arg TASKDEF_ARN "${TASKDEF_ARN}" '.Targets[0].EcsParameters.TaskDefinitionArn=$TASKDEF_ARN' > "${WORKSPACE}/scheduled-task-def.json"
cat "${WORKSPACE}/scheduled-task.json"
aws events put-targets --rule ${SCHEDULED_TASK_NAME} --cli-input-json "file://${WORKSPACE}/scheduled-task.json"

tsukiy0