Skip to content

Instantly share code, notes, and snippets.

@shakyaabiral
Created January 29, 2020 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakyaabiral/7276364577dbe0e8010c76587993e7ff to your computer and use it in GitHub Desktop.
Save shakyaabiral/7276364577dbe0e8010c76587993e7ff to your computer and use it in GitHub Desktop.
Run Ecs Task get log and exit code
run_ecs_task(){
COMMAND="$1"
ClUSTER="$2"
TASKDEF="$3"
TASK_INFO=$(aws ecs run-task \
--region ap-southeast-2 \
--cluster "$CLUSTER" \
--task-definition "$TASKDEF" \
--overrides '{"containerOverrides": [{"name": "'"$CONTAINERNAME"'", "command": ["sh", "-c", "'"$COMMAND"'"]}]}')
TASK_ARN=$(echo "$TASK_INFO" | jq --raw-output ".tasks[0].taskArn")
# wait for task to finish
aws ecs wait tasks-stopped --tasks "$TASK_ARN" --cluster "$CLUSTER"
# get logs
TASKID=$(echo "$TASK_ARN" | sed 's/.*\///')
LOG_GROUP_NAME="[log_group_name]"
LOG_STREAM_NAME="[log_stream_name]"
log=$(aws logs get-log-events \
--log-group-name "$LOG_GROUP_NAME" \
--log-stream-name "$LOG_STREAM_NAME" \
| jq -r '.events[].message')
echo "$log"
EXIT_CODE=$(aws ecs describe-tasks --tasks "$TASK_ARN" --cluster "$CLUSTER" | jq '.tasks[0].containers[0].exitCode')
return "$EXIT_CODE"
}
run_ecs_task "ls -l" "CLUSTERNAME" "TASKDEFARN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment