Skip to content

Instantly share code, notes, and snippets.

@mhart
Created December 4, 2018 02:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhart/95cfd6e23ee43e07a66dac6da72b96dd to your computer and use it in GitHub Desktop.
Save mhart/95cfd6e23ee43e07a66dac6da72b96dd to your computer and use it in GitHub Desktop.
Out-of-the-box AWS Lambda custom runtime (implemented in bash!)
#!/bin/sh
set -euo pipefail
# Handler format: <script_name>.<function_name>
# The script file <script_name>.sh must be located in
# the same directory as the bootstrap executable.
source $(dirname "$0")/"$(echo $_HANDLER | cut -d. -f1).sh"
while true
do
# Request the next event from the Lambda Runtime
HEADERS="$(mktemp)"
EVENT_DATA=$(curl -v -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
INVOCATION_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
# Execute the handler function from the script
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# Send the response to Lambda Runtime
curl -v -sS -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$INVOCATION_ID/response" -d "$RESPONSE"
done
function handler () {
EVENT_DATA=$1
RESPONSE="{\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}"
echo $RESPONSE
}
@mhart
Copy link
Author

mhart commented Dec 4, 2018

To create a zip bundle that you can deploy to Lambda:

zip lambda.zip bootstrap hello.sh

(and then use hello.handler as the Lambda handler)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment