Skip to content

Instantly share code, notes, and snippets.

@shirou
Last active May 9, 2024 05:58
Show Gist options
  • Save shirou/b11629c91d8a559113bc57e0893deff7 to your computer and use it in GitHub Desktop.
Save shirou/b11629c91d8a559113bc57e0893deff7 to your computer and use it in GitHub Desktop.
AWS Lambda bash CLI with own Docker container image
#!/usr/bin/env bash
# https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/runtimes-walkthrough.html
set -euo pipefail
# Initialization - load function handler
source $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh"
# Processing
while true
do
HEADERS="$(mktemp)"
# Get an event. The HTTP request will block until one is received
EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
# Extract request ID by scraping response headers received above
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
# Run the handler function from the script
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# Send the response
curl -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response" -d "$RESPONSE"
done
FROM debian:trixie-slim
ENV LAMBDA_TASK_ROOT=/var/task
ENV LAMBDA_RUNTIME_DIR=/var/runtime
RUN apt update && apt install -y curl unzip groff jq \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install AWS CLI
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
&& unzip awscliv2.zip \
&& ./aws/install \
&& rm awscliv2.zip
COPY bootstrap.sh ${LAMBDA_RUNTIME_DIR}/bootstrap.sh
COPY something.sh ${LAMBDA_TASK_ROOT}/something.sh
ENTRYPOINT [ "/var/runtime/bootstrap.sh" ]
CMD [ "something.handler" ]
#!/usr/bin/env bash
set -eu
# this function will be called.
function handler () {
echo "{\"statusCode\": 200, \"body\": \"success\"}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment