AWS Lambda here (Node 16.x/JavaScript). I have the following project directory structure:
my-lambda/
src/
common/
simple.js
lambda/
index.js <-- entry point for lambda
Where common.simple.js
is:
module.exports.soSimple = function() {
console.log("doing some simple stuff...");
};
And where lambda/index.js
is:
exports.checkHealth = async (event) => {
soSimple = require('../common').soSimple;
let response = {};
try {
soSimple();
response = {
statusCode: 200,
body: JSON.stringify(items)
};
} catch (e) {
response = {
statusCode: 400,
body: "Unable to do even the simplest of things"
};
}
console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
return response;
}
When I deploy this Lambda and hit the API Gateway sitting in front of it, I get back a 502 from the gateway. When I look at the Lambda logs, I see:
{
"errorType": "Error",
"errorMessage": "Cannot find module '../common'nRequire stack:n- /var/task/src/lambda/index.jsn- /var/runtime/index.mjs",
"code": "MODULE_NOT_FOUND",
"requireStack": [
"/var/task/src/lambda/index.js",
"/var/runtime/index.mjs"
],
"stack": [
"Error: Cannot find module '../common'",
"Require stack:",
"- /var/task/src/lambda/index.js",
"- /var/runtime/index.mjs",
" at Function.Module._resolveFilename (node:internal/modules/cjs/loader:995:15)",
" at Function.Module._load (node:internal/modules/cjs/loader:841:27)",
" at Module.require (node:internal/modules/cjs/loader:1067:19)",
" at require (node:internal/modules/cjs/helpers:103:18)",
" at Runtime.exports.checkHealth [as handler] (/var/task/src/lambda/index.js:3:25)",
" at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1085:29)"
]
}
What do I need to do in the code (without changing the file/folder structure) so that common/simple.js#soSimple
can be used inside my Lambda event handler when the Lambda is running both warm and cold?