LoginSignup
2
1

More than 3 years have passed since last update.

Lambdaのカスタムランタイムを試す

Posted at

はじめに

いまさらながらLambdaのカスタムランタイムを試してみます。
https://docs.aws.amazon.com/lambda/latest/dg/runtimes-walkthrough.html

手順

関数の作成
image.png

-から作成
image.png

ランタイム>デフォルトのブートストラップを使用する。>関数の作成
image.png

※ユーザ独自のブートストラップも使用可能です。
ブートストラップは、初期化処理と無限ループでイベントが来るとLambda関数を実行し、レスポンスを返す定義をします。

bootsrapとhello.shが作成されています。

image.png

bootsrap


#!/bin/sh
set -euo pipefail

# Handler format: <script_name>.<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

hello.sh

function handler () {
    EVENT_DATA=$1

    RESPONSE="{\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}"
    echo $RESPONSE
}

テスト

image.png

投稿内容は私個人の意見であり、所属企業・部門見解を代表するものではありません。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1