0

I'm trying to get the result of an invoked Lambda function.
I invoked it with AWS IoT and don't know how to get the message back to AWS IoT.

For this I did a small Node.js code which just adds two numbers and should return the result with the callback. The function is invoked correctly, because it writes the sum to CloudWatch.

All in all, my question is how can I get my result back to AWS IoT? Have I already gotton this with the callback and how can I access it? I am new to AWS and don't understand the callback logic correctly.

Here is my Lambda function code:

 
'use strict'

exports.handler = (event, context, callback) => {

let number1 = event.number1;
let number2 = event.number2;

let sum = number1 + number2; 

console.log('Sum: ' + sum);

callback(null, sum);

};

P.S. I already tried JSON.stringify for the sum but it didn't work.

1 Answers1

0

Note: Integers and floats are not compatible with JSON.stringify().

Create a json object with your data to pass in as the second parameter to your callback function.

data = {"sum" : sum};
callback(null, data);

Reference:

  • error – is an optional parameter that you can use to provide results of the failed Lambda function execution. When a Lambda function succeeds, you can pass null as the first parameter.
  • result – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.stringify compatible. If an error is provided, this parameter is ignored.

-- AWS documentation: Using the Callback Parameter

cOborski
  • 134
  • 1
  • 15