Skip to content

Instantly share code, notes, and snippets.

@mryhryki
Last active January 11, 2022 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mryhryki/8e43c52ddfc13307bbd89788e95f799e to your computer and use it in GitHub Desktop.
Save mryhryki/8e43c52ddfc13307bbd89788e95f799e to your computer and use it in GitHub Desktop.
Custom AWS Lambda runtime: provide your own bootstrap

Custom AWS Lambda runtime: provide your own bootstrap

A runtime is a program that runs a Lambda function's handler method when the function is invoked. The runtime can be included in your function's deployment package, or in a layer. Learn more about custom Lambda runtimes.

For reference, this function includes a sample Bash runtime in bootstrap.sample and a corresponding handler file hello.sh.sample. As a next step, you should provide your own bootstrap by either adding a layer implementing a custom runtime or including a bootstrap file in this function's deployment package.

#!/bin/sh
set -euo pipefail
# Handler format: <script_name>.<bash_function_name>
#
# The script file <script_name>.sh must be located at the root of your
# function's deployment package, alongside this 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
# Show all engironment variables
printenv
RESPONSE="{\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}"
echo $RESPONSE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment