AWS Lambda の終了パターン2020年9月12日 | |
はじめにAWS Lambda 関数 (Node.js) の終了パターンについて。 AWS Lambda の終了パターンreturnLambda は何を返してもよい。
exports.handler = async (event) => {
return "Hello!";
}
それっぽく次のように返してもよい。
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify("Hello!"),
};
return response;
};
contextcontext を使う方法もある。
exports.handler = async (event, context) => {
context.succeed("Hello!");
};
これは呼び出された時点で突然終わる。 エラーの場合は代わりに context.fail() を使う。
context.fail(new Error("Error!"));
callbackcallback を使う方法もある。
exports.handler = async (event, context, callback) => {
callback(null, "Hello!");
};
これは他の処理が終わるまで待つ。 エラーの場合は第1引数を使う。
callback(new Error("Error!"));
| |
| PENGUINITIS | |