node.jsaws-lambdaaws-serverless

AWS Lambda not reading my serverless function in Node


I have deployed a NodeJs server to AWS Lambda and it is being triggered by an API gateway. However, I am getting the following error in my live tail:

Error:

"errorType": "TypeError",
"errorMessage": "server is not a function",
"stack": [
"TypeError: server is not a function",
" at Runtime.handler (file:///var/task/index.js:14:44)",
" at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
]
}

My index.js file looks like this:

import express from 'express';
import routes from './startup/routes.js';
import db from './startup/db.js';
import { createServer } from 'aws-serverless-express';

const app = express();

routes(app);
db();

// const port = process.env.PORT || 8080;
// app.listen(port, () => console.log(`Listening on port ${port}...`));
const server = createServer(app);
export const handler = (event, context) => server(event, context);

I am absolutely new to AWS and serverless in NodeJs


Solution

  • According to aws-serverless-express document, you have to use proxy to handle the event:

    // ...
    import { createServer, proxy } from 'aws-serverless-express';
    
    // ...
    export const handler = (event, context) => { proxy(server, event, context) };