Node.js
AWS
docker
lambda

仮想環境で AWS Lambda の検証を行う

More than 1 year has passed since last update.

docker インストール

yum install -y docker

docker 起動

CentOS 6 系

service docker start

CentOS 7 系

systemctl start docker

準備

適当な作業ディレクトリを掘って移動する

mkdir /home/lambda
cd /home/lambda

event の入力を適当に用意する (Hello World をそのままコピペ)

vim input.json
input.json
{
  "key3": "value3",
  "key2": "value2",
  "key1": "value1"
}

実行スクリプトを適当に書く

vim index.js
index.js
exports.handler = ( event, context, callback ) => {
    console.log( 'event: ' + JSON.stringify( event, null, 2 ) );
    console.log( 'context: ' + JSON.stringify( context, null, 2 ) );
};

docker を起動して実行

docker run -v "$PWD":/var/task lambci/lambda index.handler $(printf '%s' $(cat input.json))

初回起動時はイメージのダウンロードとかがあるのでめっちゃ時間がかかる。

実行結果

START RequestId: bfb50b8e-04c3-1187-2d98-444e21663fea Version: $LATEST
2017-10-03T10:15:42.553Z        bfb50b8e-04c3-1187-2d98-444e21663fea    event: {
  "key3": "value3",
  "key2": "value2",
  "key1": "value1"
}
2017-10-03T10:15:42.554Z        bfb50b8e-04c3-1187-2d98-444e21663fea    context: {
  "callbackWaitsForEmptyEventLoop": true,
  "logGroupName": "/aws/lambda/test",
  "logStreamName": "2017/10/03/[$LATEST]af9c9a780a74aac9a5eff6b5e1bb8d80",
  "functionName": "test",
  "memoryLimitInMB": "1536",
  "functionVersion": "$LATEST",
  "invokeid": "bfb50b8e-04c3-1187-2d98-444e21663fea",
  "awsRequestId": "bfb50b8e-04c3-1187-2d98-444e21663fea",
  "invokedFunctionArn": "arn:aws:lambda:us-east-1:1586600703:function:test"
}
END RequestId: bfb50b8e-04c3-1187-2d98-444e21663fea
REPORT RequestId: bfb50b8e-04c3-1187-2d98-444e21663fea  Duration: 4.98 ms       Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 19 MB  

null

Lambda でも実行して見る

START RequestId: xxxxxxxxxx-a824-11e7-9d62-xxxxxxxxxx Version: $LATEST
2017-10-03T10:18:41.539Z    xxxxxxxxxx-a824-11e7-9d62-xxxxxxxxxx    event: {
  "key3": "value3",
  "key2": "value2",
  "key1": "value1"
}
2017-10-03T10:18:41.544Z    xxxxxxxxxx-a824-11e7-9d62-xxxxxxxxxx    context: {
  "callbackWaitsForEmptyEventLoop": true,
  "logGroupName": "/aws/lambda/testOnLambda",
  "logStreamName": "2017/10/03/[$LATEST]xxxxxxxxxxxxxxxxxxxxxx",
  "functionName": "testOnLambda",
  "memoryLimitInMB": "128",
  "functionVersion": "$LATEST",
  "invokeid": "xxxxxxxxxx-a824-11e7-9d62-xxxxxxxxxx",
  "awsRequestId": "xxxxxxxxxx-a824-11e7-9d62-xxxxxxxxxx",
  "invokedFunctionArn": "arn:aws:lambda:ap-northeast-1: xxxxxxxxxx:function:testOnLambda"
}
END RequestId: xxxxxxxxxx-a824-11e7-9d62-xxxxxxxxxx
REPORT RequestId: xxxxxxxxxx-a824-11e7-9d62-xxxxxxxxxx  Duration: 27.69 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 19 MB

適当にマスクかけたけど、ほぼ同じ結果だと思う。