Skip to content

Commit 61eac56

Browse files
author
Graham Krizek
committed
Working on Response functions
1 parent b9cb425 commit 61eac56

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

bootstrap

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,56 @@ set -euo pipefail
44

55
RUNTIME_PATH="2018-06-01/runtime"
66

7+
# Response
78
sendInitError () {
8-
ERROR_MESSAGE=$1
9-
ERROR_TYPE=$2
10-
ERROR="{\"errorMessage\" : \"$ERROR_MESSAGE\", \"errorType\" : \"$ERROR_TYPE\"}"
11-
curl -X POST -d "$ERROR" "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/$REQUEST_ID/error" > /dev/null
12-
echo "ERROR" >&2
13-
exit 1
9+
ERROR_MESSAGE=$1
10+
ERROR_TYPE=$2
11+
ERROR="{\"errorMessage\": \"$ERROR_MESSAGE\", \"errorType\": \"$ERROR_TYPE\"}"
12+
echo $ERROR >&2
13+
curl -sS -X POST -d "$ERROR" "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/init/error" > /dev/null
14+
exit 1
15+
}
16+
17+
sendRuntimeError () {
18+
ERROR_MESSAGE=$1
19+
ERROR_TYPE=$2
20+
REQUEST_ID=$3
21+
ERROR="{\"errorMessage\": \"$ERROR_MESSAGE\", \"errorType\": \"$ERROR_TYPE\"}"
22+
echo $ERROR >&2
23+
curl -sS -X POST -d "$ERROR" "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/${REQUEST_ID}/error"
24+
}
25+
26+
sendResponse () {
27+
RESPONSE=$1
28+
curl -sS -X POST -d "$RESPONSE" "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/${REQUEST_ID}/response" > /dev/null
29+
}
30+
31+
# Make sure file exists and it can be sourced
32+
{
33+
[[ ! -f $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh" ]] && . $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh"
34+
} || {
35+
sendInitError "Failed to load handler '$(echo $_HANDLER | cut -d. -f2)' from module '$(echo $_HANDLER | cut -d. -f1)'" "InvalidHandlerException"
1436
}
1537

1638
# Initialization
1739
source $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh"
18-
if [[ $? -ne 0 ]]; then
19-
sendInitError "Handler Not Found" "InvalidHandlerException"
20-
fi
2140

2241
# Processing
2342
while true
2443
do
2544
HEADERS="$(mktemp)"
26-
EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/next")
45+
EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocaion/next")
46+
if [[ $? != 0 ]]; then
47+
echo "INIT ERROR" >&2
48+
sendInitError "There was a problem retriving invocations" "InternalRuntimeException"
49+
fi
2750
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
2851
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
29-
echo $?
30-
echo "RESPONSE::::"
31-
curl -sS -X POST -d "$RESPONSE" "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/${REQUEST_ID}/response" > /dev/null
52+
if [[ $? -eq 0 ]]; then
53+
echo "GOOD RESPONSE" >&2
54+
sendResponse $RESPONSE
55+
else
56+
echo "BAD RESPONSE" >&2
57+
sendRuntimeError "ErrorMessage" "ErrorType" $REQUEST_ID
58+
fi
3259
done

test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#!/bin/bash
1+
#!/bin/bash -xe
22

33
function handler () {
44
EVENT_DATA=$1
55

66
echo "this is my function" >&2
7-
7+
git ls
88
echo $EVENT_DATA
99
}

0 commit comments

Comments
 (0)