Invoking lambda function from a nodejs API proxy

Hi,

I am trying to invoke a lambda function from nodejs API proxy. I am using aws-sdk npm library. The nodejs code works fine when executed standalone, but on edge i am getting the following error

{
    "fault": {
        "faultstring": "Script node executed prematurely: Script server.js exited with status code 0",
        "detail": {
            "errorcode": "scripts.node.runtime.ScriptExitedError"
        }
    }
}

Has anyone faced a similar challenge ?

My server.js looks like this

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var AWS = require('aws-sdk');
AWS.config.accessKeyId='xxxxxxxxxxxxxxxxxx';
AWS.config.secretAccessKey='xxxxxxxxx';
AWS.config.region = 'xxxxxxxxx';
var lambda = new AWS.Lambda();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
app.disable('etag');


app.post('/lambdatest', function(req, res) {


    var params = {
   FunctionName: 'myLambdaFunction',
   InvocationType : 'RequestResponse',
   Payload: JSON.stringify(
     {
       "body" : {
          "key":req.body.key
     }
    })};
lambda.invoke(params, function(err, data) {
   if (err)
     {
      res.status(500);
      res.set('Content-Type', 'application/json');
      res.send(err);
     }
   else
    {
      res.status(200);
      res.set('Content-Type', 'application/json');
      res.send(data.Payload);
    }
  });
    
});  
Solved Solved
1 1 2,580
1 ACCEPTED SOLUTION

I've done this previously and had no problems.

If you use a try...catch in your javascript code, you may get more diagnostic information about the source of the problem. The full stacktrace, the error message and so on. See here for a hint on that.

You can also examine the nodejs logs for the API Proxy, to see if there is any other information there.

You'll want to make sure the API proxy has been properly deployed with all the NPM dependencies satisfied. There have been some problems with that reported here on community, recently. I haven't fully understood the cause of those issues yet.

View solution in original post

1 REPLY 1

I've done this previously and had no problems.

If you use a try...catch in your javascript code, you may get more diagnostic information about the source of the problem. The full stacktrace, the error message and so on. See here for a hint on that.

You can also examine the nodejs logs for the API Proxy, to see if there is any other information there.

You'll want to make sure the API proxy has been properly deployed with all the NPM dependencies satisfied. There have been some problems with that reported here on community, recently. I haven't fully understood the cause of those issues yet.