11#! /bin/sh
22
3- set -u
3+ # set -u
44
5+ # Constants
56RUNTIME_PATH=" 2018-06-01/runtime"
67
7- # Response
8+ # Send initialization error to Lambda API
89sendInitError () {
910 ERROR_MESSAGE=$1
1011 ERROR_TYPE=$2
1112 ERROR=" {\" errorMessage\" : \" $ERROR_MESSAGE \" , \" errorType\" : \" $ERROR_TYPE \" }"
12- echo $ERROR >&2
1313 curl -sS -X POST -d " $ERROR " " http://${AWS_LAMBDA_RUNTIME_API} /${RUNTIME_PATH} /init/error" > /dev/null
1414}
1515
16+ # Send runtime error to Lambda API
1617sendRuntimeError () {
17- ERROR_MESSAGE =$1
18- ERROR_TYPE =$2
19- REQUEST_ID =$3
20- ERROR= " { \" errorMessage \" : \" $ERROR_MESSAGE \" , \" errorType \" : \" $ERROR_TYPE \" } "
21- echo $ERROR >&2
18+ REQUEST_ID =$1
19+ ERROR_MESSAGE =$2
20+ ERROR_TYPE =$3
21+ STACK_TRACE= $4
22+ ERROR= " { \" errorMessage \" : \" $ERROR_MESSAGE \" , \" errorType \" : \" $ERROR_TYPE \" , \" stackTrace \" : \" $STACK_TRACE \" } "
2223 curl -sS -X POST -d " $ERROR " " http://${AWS_LAMBDA_RUNTIME_API} /${RUNTIME_PATH} /invocation/${REQUEST_ID} /error" > /dev/null
2324}
2425
26+ # Send successful response to Lambda API
2527sendResponse () {
26- RESPONSE=$1
28+ REQUEST_ID=$1
29+ RESPONSE=$2
2730 curl -sS -X POST -d " $RESPONSE " " http://${AWS_LAMBDA_RUNTIME_API} /${RUNTIME_PATH} /invocation/${REQUEST_ID} /response" > /dev/null
2831}
2932
4144 HEADERS=" $( mktemp) "
4245 EVENT_DATA=$( curl -sS -LD " $HEADERS " -X GET " http://${AWS_LAMBDA_RUNTIME_API} /${RUNTIME_PATH} /invocation/next" )
4346 REQUEST_ID=$( grep -Fi Lambda-Runtime-Aws-Request-Id " $HEADERS " | tr -d ' [:space:]' | cut -d: -f2)
44- RESPONSE=$( $( echo " $_HANDLER " | cut -d. -f2) " $EVENT_DATA " )
45- # echo $EXIT_CODE >&2
46- git ls
47- echo $?
48- echo $? >&2
49- # echo $RESPONSE >&2
50- if [[ $? -eq 0 ]]; then
51- echo " GOOD RESPONSE" >&2
52- sendResponse $RESPONSE
47+ RESPONSE=$( $( echo " $_HANDLER " | cut -d. -f2) " $EVENT_DATA " 2>&1 > /dev/null)
48+ # Goal here is to only capture STDERR to the $RESPONSE variable and allow STDOUT to be logged like normal.
49+ # Then for successful response messages we set them to a global variable $LAMBDA_RETURN_VALUE
50+ EXIT_CODE=$?
51+ if [[ $EXIT_CODE -eq " 0" ]]; then
52+ sendResponse $REQUEST_ID $LAMBDA_RETURN_VALUE
5353 else
54- echo " BAD RESPONSE" >&2
55- sendRuntimeError " ErrorMessage" " ErrorType" $REQUEST_ID
54+ echo " failure"
55+ echo $RESPONSE
56+ sendRuntimeError $REQUEST_ID " Exited with code $EXIT_CODE " " RuntimeErrorException" " $RESPONSE "
5657 fi
5758done
0 commit comments