-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Anaconda-Platform/lambda
Enable AWS Lambda
- Loading branch information
Showing
19 changed files
with
828 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
if [ -e /opt/app-root/src/.assembled ]; then | ||
if [[ $1 == "/usr/libexec/s2i/run" ]]; then | ||
exec tini -g -- "$@" | ||
else | ||
exec tini -g -- anaconda-project run "$@" | ||
fi | ||
else | ||
exec tini -g -- "$@" | ||
fi |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
|
||
ARGS=${@:-"/usr/libexec/s2i/run"} | ||
|
||
if [ -e /opt/app-root/src/.assembled ]; then | ||
if [[ $ARGS == "/usr/libexec/s2i/run" ]]; then | ||
if [ -v AWS_LAMBDA_RUNTIME_API ]; then | ||
exec /usr/libexec/s2i/run-lambda "$ARGS" | ||
else | ||
exec /opt/aws/aws-lambda-rie /usr/libexec/s2i/run-lambda "$ARGS" | ||
fi | ||
else | ||
if [ -v AWS_LAMBDA_RUNTIME_API ]; then | ||
exec /usr/libexec/s2i/run-lambda anaconda-project run "$ARGS" | ||
else | ||
exec /opt/aws/aws-lambda-rie /usr/libexec/s2i/run-lambda anaconda-project run "$ARGS" | ||
fi | ||
fi | ||
else | ||
exec "$ARGS" | ||
fi |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
|
||
ARGS=${@:-"/usr/libexec/s2i/run"} | ||
|
||
if [ -e /opt/app-root/src/.assembled ]; then | ||
if [[ $ARGS == "/usr/libexec/s2i/run" ]]; then | ||
if [ -v AWS_LAMBDA_RUNTIME_API ]; then | ||
exec "$ARGS" | ||
else | ||
exec /opt/aws/aws-lambda-rie "$ARGS" | ||
fi | ||
else | ||
if [ -v AWS_LAMBDA_RUNTIME_API ]; then | ||
exec anaconda-project run "$ARGS" | ||
else | ||
exec /opt/aws/aws-lambda-rie anaconda-project run "$ARGS" | ||
fi | ||
fi | ||
else | ||
exec "$ARGS" | ||
fi |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/bin/bash | ||
|
||
set -uo pipefail | ||
|
||
RUN_COMMAND=${@} | ||
|
||
# Constants | ||
RUNTIME_PATH="2018-06-01/runtime" | ||
mkdir -p /tmp/.aws | ||
touch /tmp/.aws/config | ||
# export HOME="/tmp" | ||
# export AWS_CONFIG_FILE="/tmp/.aws/config" | ||
|
||
# Send initialization error to Lambda API | ||
sendInitError () { | ||
ERROR_MESSAGE=$1 | ||
ERROR_TYPE=$2 | ||
ERROR="{\"errorMessage\": \"$ERROR_MESSAGE\", \"errorType\": \"$ERROR_TYPE\"}" | ||
curl -sS -X POST -d "$ERROR" "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/init/error" > /dev/null | ||
} | ||
|
||
# Send runtime error to Lambda API | ||
sendRuntimeError () { | ||
REQUEST_ID=$1 | ||
ERROR_MESSAGE=$2 | ||
ERROR_TYPE=$3 | ||
STACK_TRACE=$4 | ||
ERROR="{\"errorMessage\": \"$ERROR_MESSAGE\", \"errorType\": \"$ERROR_TYPE\", \"stackTrace\": \"$STACK_TRACE\"}" | ||
curl -sS -X POST -d "$ERROR" "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/${REQUEST_ID}/error" > /dev/null | ||
} | ||
|
||
# Send successful response to Lambda API | ||
sendResponse () { | ||
REQUEST_ID=$1 | ||
REQUEST_RESPONSE_FILE=$2 | ||
cat $REQUEST_RESPONSE_FILE | curl -sS -X POST -d @- "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/${REQUEST_ID}/response" > /dev/null | ||
} | ||
|
||
# # Make sure handler function exists | ||
# type "$(echo $_HANDLER | cut -d. -f2)" > /dev/null 2>&1 | ||
# if [[ ! $? -eq "0" ]]; then | ||
# sendInitError "Failed to load handler '$(echo $_HANDLER | cut -d. -f2)' from module '$(echo $_HANDLER | cut -d. -f1)'. Function '$(echo $_HANDLER | cut -d. -f2)' does not exist." "InvalidHandlerException" | ||
# exit 1 | ||
# fi | ||
|
||
# Processing | ||
while true | ||
do | ||
HEADERS="/tmp/headers-$(date +'%s')" | ||
STDOUT="/tmp/stdout-$(date +'%s')" | ||
STDERR="/tmp/stderr-$(date +'%s')" | ||
touch $HEADERS | ||
touch $STDOUT | ||
touch $STDERR | ||
EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/${RUNTIME_PATH}/invocation/next") | ||
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2) | ||
# Export some additional context | ||
export AWS_LAMBDA_REQUEST_ID=$REQUEST_ID | ||
export AWS_LAMBDA_DEADLINE_MS=$(grep -Fi Lambda-Runtime-Deadline-Ms "$HEADERS" | tr -d '[:space:]' | cut -d: -f2) | ||
export AWS_LAMBDA_FUNCTION_ARN=$(grep -Fi Lambda-Runtime-Invoked-Function-Arn "$HEADERS" | cut -d" " -f2) | ||
export AWS_LAMBDA_TRACE_ID=$(grep -Fi Lambda-Runtime-Trace-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2) | ||
# Execute the command | ||
$RUN_COMMAND "$EVENT_DATA" 1> $STDOUT 2> $STDERR | ||
EXIT_CODE=$? | ||
cat $STDOUT | ||
cat $STDERR | ||
# Respond to Lambda API | ||
if [[ $EXIT_CODE -eq "0" ]]; then | ||
sendResponse "$REQUEST_ID" "$STDOUT" | ||
else | ||
sendRuntimeError "$REQUEST_ID" "Exited with code $EXIT_CODE" "RuntimeErrorException" "$(cat $STDERR)" | ||
fi | ||
# Clean up | ||
rm -f -- "$HEADERS" | ||
rm -f -- "$STDOUT" | ||
rm -f -- "$STDERR" | ||
unset HEADERS | ||
unset STDOUT | ||
unset STDERR | ||
done |
Oops, something went wrong.