1
votes

I have a lambda function running on amazon aws cloud. Now I want to make a node.js script to send data from my local system to aws lambda and use callback function to print the same value sent from my node.js code.

Now, to trigger my lambda function from my node.js code, I'm using the following code:

var AWS = require('aws-sdk');

// you shouldn't hardcode your keys in production! See http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html

AWS.config.update({accessKeyId: 'myaccessKeyId', secretAccessKey: 'mysecretAccessKey',region:'region',correctClockSkew: true});

var lambda = new AWS.Lambda({apiVersion: '2015-03-31'});
var params = {
  FunctionName: 'myLambdaFunction', /* required */
  Payload: 'true',
};
lambda.invoke(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

I am new to this lambda function concept, so can anyone help me by telling how to send data to the required lambda function from the above mentioned code? Using the above code I am able to trigger my lambda function and I am getting in my node app whatever I am printing in it's payload.

I am getting the following error, when I am using a custom string value ( other than 'true', 'false' or 'null') in payload and the api version which I am using is:apiVersion: '2015-03-31' , and the aws-sdk node module was recently installed, so I guess it's up-to-date. Error message:

{ InvalidRequestContentException: Could not parse request body into json: Unrecognized token 'custom_data': was expecting ('true', 'false' or 'null') at [Source: [B@7d2214ec; line: 1, column: 23] at Object.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/json.js:43:27) at Request.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/rest_json.js:37:8) at Request.callListeners (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:105:20) at Request.emit (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:77:10) at Request.emit (/usr/lib/node_modules/aws-sdk/lib/request.js:668:14) at Request.transition (/usr/lib/node_modules/aws-sdk/lib/request.js:22:10) at AcceptorStateMachine.runTo (/usr/lib/node_modules/aws-sdk/lib/state_machine.js:14:12) at /usr/lib/node_modules/aws-sdk/lib/state_machine.js:26:10 at Request. (/usr/lib/node_modules/aws-sdk/lib/request.js:38:9) at Request. (/usr/lib/node_modules/aws-sdk/lib/request.js:670:12) message: 'Could not parse request body into json: Unrecognized token \'custom_data\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B@7d2214ec; line: 1, column: 23]', code: 'InvalidRequestContentException', time: 2017-01-16T16:48:38.514Z, requestId: '3bee0e2c-dd39-11e6-9df3-5f7a24f73b9d', statusCode: 400, retryable: false, retryDelay: 26.112914258191733 } 'InvalidRequestContentException: Could not parse request body into json: Unrecognized token \'custom_data\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B@7d2214ec; line: 1, column: 23]\n at Object.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/json.js:43:27)\n at Request.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/rest_json.js:37:8)\n at Request.callListeners (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\n at Request.emit (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:77:10)\n at Request.emit (/usr/lib/node_modules/aws-sdk/lib/request.js:668:14)\n at Request.transition (/usr/lib/node_modules/aws-sdk/lib/request.js:22:10)\n at AcceptorStateMachine.runTo (/usr/lib/node_modules/aws-sdk/lib/state_machine.js:14:12)\n at /usr/lib/node_modules/aws-sdk/lib/state_machine.js:26:10\n at Request. (/usr/lib/node_modules/aws-sdk/lib/request.js:38:9)\n at Request. (/usr/lib/node_modules/aws-sdk/lib/request.js:670:12)'

Kindly help.

2

2 Answers

1
votes

You are currently sending data to the Lambda function. The data you are sending is the string 'true'. You send data via the Payload property. From the documentation:

Payload — (Buffer, Typed Array, Blob, String)

JSON that you want to provide to your Lambda function as input.

1
votes

To extend the the answer provided by Mark B, have the data that will be loaded into payload constructed as:

var data =  {
    key: value,
    key: value,
    key: value
}

var datapayload = JSON.stringify(data);

var datalambda= {
    FunctionName: 'LambdaFunctionName',
    InvocationType: 'RequestResponse',
    Payload: datapayload,
    LogType: 'None'
};

// create variable to hold data returned by the Lambda function
var returndata;

lambda.invoke(datalambda, function(error, data) {
    if (error) {
        console.log(error);
    } 
    else {
        returndata = JSON.parse(data.Payload);
        var log = JSON.stringify(returndata);
        console.log(log);
    }