Checking for the result of the AWS CLI 'run-task' command, task stopped succesfully or from an error?

General Linux topics
Post Reply
admin
Site Admin
Posts: 50
Joined: Sun Aug 08, 2021 7:49 am

Checking for the result of the AWS CLI 'run-task' command, task stopped succesfully or from an error?

Post by admin »

I'm currently moving an application off of static EC2 servers to ECS, as until now the release process has been ssh'ing into the server to git pull/migrate the database.

I've created everything I need using terraform to deploy my code from my organisations' Elastic Container Registry. I have a cluster, some services and task definitions.

I can deploy the app successfully for any given version now, however my main problem is finding a way to run migrations.

My approach so far has been to split the application into 3 services, I have my 'web' service which handles all HTTP traffic (serving the frontend, responding to API requests), my 'cron' service which handles things like sending emails/push notifications on specific times/events and my 'migrate' service which is just the 'cron' service but with the entryPoint to the container overwritten to just run the migrations (as I don't need any of the apache2 stuff for this container, and I didn't see reason to make another one for just migrations).

The problem I had with this was the 'migrate' service would constantly try and schedule more tasks for migrating the database, even though it only needed to be done once. So I've scrapped it as a service and kept it as a task definition however, so that I can still place it into my cluster.

As part of the deploy process I'm writing, I run that task inside the cluster via a bash script so I can wait until the migrations finish before deciding whether to take the application out of maintenance mode (if the migrations fail) or to deploy the new 'web'/'cron' containers once the migration has been completed.

Currently this is inside a shell script (ran by Github actions) that looks like this:

#!/usr/bin/env bash

CLUSTER_NAME=$1

echo $CLUSTER_NAME

OUTPUT=`aws ecs run-task --cluster ${CLUSTER_NAME} --task-definition saas-app-migrate`

if [$? -n 0]; then
>&2 echo $OUTPUT
exit 1
fi

TASKS=`echo $OUTPUT | jq '.tasks[].taskArn' | jq @sh | sed -e "s/'//g" | sed -e 's/"//g'`

for task in $TASKS
do
# check for task to be done
done
Because $TASKS contains the task and of any tasks that have been spawned by this, I am freely able to query the task however I don't know what information I'm looking for.

The AWS documentation says I should use the 'describe-task' command to then find out why a task has reached the 'STOPPED' status, as it provides a 'stopCode' and 'stoppedReason' property in the response. However, it doesn't say what these values would be if it was succesfully stopped? I don't want to have to introduce a manual step in my deployment where I wait until the migrations are done - with the application not being usable - to then tell my release process to continue.

Is there a link to documentation I might have missed with the values I'm searching for, or an alternate way to handle this case?
Post Reply