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?
You can view and search these logs in the console. Open the Amazon ECS console at https://console.aws.amazon.com/ecs/ . On the Clusters page, select the cluster that contains the task to view. On the Cluster: cluster_name page, choose Tasks and select the task to view.
To connect to your container instanceOpen the Amazon ECS console at https://console.aws.amazon.com/ecs/ . Select the cluster that hosts your container instance. On the Cluster page, choose ECS Instances. On the Container Instance column, select the container instance to connect to.
Essential container in task exitedIf the essential parameter of a container is marked as true and that container fails or stops for any reason, then all other containers that are part of that task are stopped.
The exit code is available through the DescribeTasks
API as exitCode
for each of the containers.
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
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)
# exit with the same code
exit $TASK_EXIT_CODE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With