LoginSignup
6
8

More than 5 years have passed since last update.

[AWS][Lambda] ローカルで Lambda 用の nodejs スクリプトを実行する

Posted at

ローカルで Lambda 用に作ったスクリプトを実行してみたいことってありますよね。
簡易的にテストしたいときとか。
そんな時は、以下のような test.js を作成してやってます。

test.js
var event = {
  "value1": "value1"
};

var context = {
    succeed: function(data){console.log(JSON.stringify(data,' ',4));},
    fail: function(data){console.log("fail!!\n" + JSON.stringify(data,' ',4));},
    invokedFunctionArn: 'test:development',
    functionName: 'test',
    functionVersion: '$LATEST'
};
var callback = function(){};

var myLambda = require('./index');
myLambda.handler(event, context, callback);

Lambda 登録用の zip ファイルには aws-sdk は含めてないと思うんで、npm install の -g オプションでグローバル領域にインストールしておきましょう。

$ npm -g install aws-sdk

実行する時にデフォルト以外の aws-cli に登録されている configure ファイルを指定したい場合は、以下のようにして実行します。
(例えば example ってプロファイルを使いたい場合)

$ export AWS_PROFILE='example'; node test.js
6
8
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
6
8