0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node.js の Lambda 関数を ローカルでテストする (その2)

Posted at

AWS のコンソールから、ダウンロードして解凍した index.js を実行する方法です。

download_package.png

フォルダー構造

$ tree example01/
example01/
├── index.js
└── test_local.js
index.js
exports.handler = async (event) => {
    // TODO implement
    console.error("***** start example01 PM 18:49 ***")
    var rvalue = {}
    rvalue['key1'] = parseInt(event['key1'],10)
    rvalue['key2'] = parseInt(event['key2'],10)
    rvalue['key3'] = parseInt(event['key3'],10)
    rvalue['sum'] = rvalue['key1']+ rvalue['key2'] + rvalue['key3']
    rvalue['message'] = 'Hello from example01'

    const response = {
        statusCode: 200,
        body: JSON.stringify(rvalue),
    };
    console.error("***** end example01 ***")
    return response;
};
test_local.js

#! /usr/bin/node
// ---------------------------------------------------------------
//	test_local.js
//
//					Jun/06/2020
//
// ---------------------------------------------------------------
var fs = require("fs")
var example01 = require('../example01')
// ---------------------------------------------------------------
console.error ("*** 開始 *** test_local.js ***")

const event = {
	"key1": 11,
	"key2": 21,
	"key3": 31
}

const context = ""

rvalue = example01.handler(event)
console.log(rvalue)

console.error ("*** 終了 *** test_local.js ***")
// ---------------------------------------------------------------

実行結果

$ ./test_local.js 
*** 開始 *** test_local.js ***
***** start example01 PM 18:49 ***
***** end example01 ***
Promise {
  {
    statusCode: 200,
    body: '{"key1":11,"key2":21,"key3":31,"sum":63,"message":"Hello from example01"}'
  }
}
*** 終了 *** test_local.js ***
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?