https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/runtimes-walkthrough.html
-- 1. コマンド等のインストール
-- 1.1 aws cli version 2 インストール
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
-- 1.2 jqインストール
sudo yum -y install jq
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json
-- 3. ポリシーをロールにアタッチ
aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole \
--role-name role01
-- 4. Lambda関数作成
mkdir runtime-tutorial
cd runtime-tutorial
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
vim function.sh
function handler () {
EVENT_DATA=$1
echo "$EVENT_DATA" 1>&2;
RESPONSE="Echoing request: '$EVENT_DATA'"
echo $RESPONSE
}
※"$_HANDLER" = function.handler
chmod 755 function.sh bootstrap
zip ../function.zip function.sh bootstrap
cd ..
aws lambda create-function \
--function-name func01 \
--zip-file fileb://function.zip \
--handler function.handler \
--runtime provided \
--role arn:aws:iam::999999999999:role/role01
aws lambda list-functions | grep func01
aws lambda get-function --function-name func01
-- 5. 動作確認
aws lambda invoke \
--function-name func01 \
--payload '{"text":"Hello"}' \
response.txt \
--cli-binary-format raw-in-base64-out
cat response.txt
-- 6. レイヤーの作成
cd runtime-tutorial
zip ../runtime.zip bootstrap
cd ..
aws lambda publish-layer-version \
--layer-name layer01 \
--zip-file fileb://runtime.zip
aws lambda list-layers
-- 7. 関数の更新
関数でランタイムレイヤーを使用するには、レイヤーを使用するように関数を設定し、関数からランタイムコードを削除します。
aws lambda update-function-configuration \
--function-name func01 \
--layers arn:aws:lambda:ap-northeast-1:999999999999:layer:layer01:1
cd runtime-tutorial
zip ../function-only.zip function.sh
cd ..
aws lambda update-function-code \
--function-name func01 \
--zip-file fileb://function-only.zip
-- 8. 動作確認
aws lambda invoke \
--function-name func01 \
--payload '{"text":"Hello"}' \
response.txt \
--cli-binary-format raw-in-base64-out
cat response.txt
-- 9. ランタイムの更新
cd runtime-tutorial
sed -i '5iecho "## Environment variables:"\
env\
' bootstrap
cat bootstrap
zip ../runtime.zip bootstrap
cd ..
aws lambda publish-layer-version \
--layer-name layer01 \
--zip-file fileb://runtime.zip
aws lambda update-function-configuration \
--function-name func01 \
--layers arn:aws:lambda:ap-northeast-1:999999999999:layer:layer01:2
aws lambda get-function-configuration \
--function-name func01
-- 10. 動作確認
aws lambda invoke \
--function-name func01 \
--payload '{"text":"Hello"}' \
response.txt \
--cli-binary-format raw-in-base64-out
cat response.txt
-- 11. レイヤーを共有する
aws lambda add-layer-version-permission \
--layer-name layer01 \
--version-number 2 \
--principal 888888888888 \
--statement-id xaccount \
--action lambda:GetLayerVersion
aws lambda get-layer-version-policy \
--layer-name layer01 \
--version-number 2
-- 12. クリーンアップ
-- レイヤー共有の取り消し
aws lambda get-layer-version-policy \
--layer-name layer01 \
--version-number 2
aws lambda remove-layer-version-permission \
--layer-name layer01 \
--version-number 2 \
--statement-id xaccount
-- Lambda Layerの削除
aws lambda list-layers
aws lambda delete-layer-version \
--layer-name layer01 \
--version-number 1
aws lambda delete-layer-version \
--layer-name layer01 \
--version-number 2
-- Lambda関数の削除
aws lambda list-functions | grep func01
aws lambda delete-function --function-name func01
-- ロールの削除
aws iam list-roles | grep role01
aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
aws iam delete-role --role-name role01