Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an exit code from an Amazon ECS Task?

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?

like image 490
Scott Smith Avatar asked Oct 19 '16 17:10

Scott Smith


People also ask

How can I get ECS task logs?

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.

How do I connect to an ECS task?

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.

Why is essential container in task exited?

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.


2 Answers

The exit code is available through the DescribeTasks API as exitCode for each of the containers.

like image 142
Samuel Karp Avatar answered Sep 18 '22 10:09

Samuel Karp


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
like image 20
SomeGuyOnAComputer Avatar answered Sep 22 '22 10:09

SomeGuyOnAComputer