I’m using the yarn rw deploy serverless
to deploy directly to AWS. I’m having trouble with the auto-generated lambda functions not importing modules correctly. A simple example is a ‘hello’ Function with and without import { logger } from 'src/lib/logger'
.
import type { APIGatewayEvent, Context } from 'aws-lambda'
import { logger } from 'src/lib/logger'
export const handler = async (event: APIGatewayEvent, context: Context) => {
logger.info('Invoked hello function')
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: 'hello function',
}),
}
}
crashes with the error stack in CloudWatch of:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module '..\\..\\lib\\logger'\nRequire stack:\n- /var/task/api/dist/functions/hello/hello.js\n- /var/task/hello.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module '..\\..\\lib\\logger'",
"Require stack:",
"- /var/task/api/dist/functions/hello/hello.js",
"- /var/task/hello.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/Runtime.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:221:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:34)",
" at Module._compile (internal/modules/cjs/loader.js:1085:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
" at Module.load (internal/modules/cjs/loader.js:950:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)",
" at internal/main/run_main_module.js:17:47"
]
}
but removing the logger import and the logger call from the lambda Function, when deployed returns the expected {"data":"hello function"}
.
Am I missing a setting or are the zipballs not generating correctly, or?
Thanks!