How do I get an exit code from an Amazon ECS Task?
amazon-ecs, amazon-web-services, docker
Solution
This is how I'm getting the exit code of a task's specific container name
aws ecs describe-tasks \
--cluster $ECS_CLUSTER \
--tasks $TASK_ARN \
--query "tasks[0].containers[?name=='$CONTAINER_NAME'].exitCode" \
--output text
In a script
#!/bin/sh
AWS_PROFILE=default
ECS_CLUSTER=cluster_name
CONTAINER_NAME=migrate
# Run task and get its arn
# NOTE: many of the necessary cli inputs have been omitted here
TASK_ARN=$(aws ecs run-task \
--cluster $ECS_CLUSTER \
--query 'tasks[].taskArn' \
--output text | rev | cut -d'/' -f1 | rev)
# Wait for ecs task to stop
aws ecs wait tasks-stopped \
--cluster $ECS_CLUSTER \
--tasks $TASK_ARN
# Get exit code
TASK_EXIT_CODE=$(aws ecs describe-tasks \
--cluster $ECS_CLUSTER \
--tasks $TASK_ARN \
--query "tasks[0].containers[?name=='$CONTAINER_NAME'].exitCode" \
--output text)
echo "The $TASK_ARN ran in ECS cluster $ECS_CLUSTER and its $CONTAINER_NAME returned exit code $TASK_EXIT_CODE"
# exit with the same code
exit $TASK_EXIT_CODE
If there are multiple containers, it would be good to `sum` the exit codes before exiting or check each individual container separately.
Problem
When I launch tasks in Amazon AWS ECS containers, I need to recover the exit code programmatically via the Java SDK. It appears in the Amazon web interface, and in the SDK I can get a text-based failure reason, but is there a way to get the explicit exit code?