Created
January 29, 2020 23:22
-
-
Save shakyaabiral/7276364577dbe0e8010c76587993e7ff to your computer and use it in GitHub Desktop.
Run Ecs Task get log and exit code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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