I have uploaded a node js express project to AWS lambda. The following is my handler code saved as exports.js:
const
express = require('express'),
bodyParser = require('body-parser'),
request = require('request'),
app = express().use(bodyParser.json()); // creates express http server
exports.handler = function(callback){
request('http://localhost/php-rest/api.php/routes?filter=route_short_name', function(error, response, body) {
if (!error && response.statusCode == 200) {
message = JSON.stringify(JSON.parse(body));
return callback(message, false);
} else {
return callback(null, error);;
}
});
}
app.get('/exports.handler', function(req, res) {
exports.handler(function(err, data){
if(err) return res.send(err);
res.send(data);
});
});
The handler code is separate from my app.js file. I got the following error when I tested it on aws lambda:
{
"errorMessage": "Handler 'handler' missing on module 'exports'"
}
So this is your lambda function which should be there as a handler. In your code app.get() has to be handled by AWS API Gateway. because it is the method of invocation of lambda functions. You can't have the nodejs server inside the lambda function.
So the .zip file should be named as index.js since when we upload the .zip file it extracts the contents and find the handler name which we have provided. These are the .zip file contents that should be uploaded.
index.js.zip
node_modules
index.js
package.json
package-lock.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With