4

I have a NodeJS function in Lambda which calls out to a library in order to run a geospatial query in Dynamo DB.

Ultimately, I would like the results from this query to be returned by Lambda, as this Lambda function will ultimately be invoked by another and as such the results must be returned.

I am unable to return the results of the promise in a Lambda function.

I've tried rewriting the code several times, understanding promises, using async await... I've read a number of articles including https://dashbird.io/blog/aws-lambda-supports-node-version-8.10/ https://techsparx.com/software-development/aws/aws-sdk-promises.html https://medium.com/tensult/async-await-on-aws-lambda-function-for-nodejs-2783febbccd9 Getting API call in node8.10 in Lambda results in Promise <pending> and undefined

To no avail.

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB();
const ddbGeo = require('dynamodb-geo');
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'MyGeoTable');
const myGeoTableManager = new ddbGeo.GeoDataManager(config);

exports.handler = async function (event, context) {
    let data = await myGeoTableManager.queryRadius({
        RadiusInMeter: 1000,
        CenterPoint: {latitude: 51.50, longitude: -0.17}
    });
    console.log(data);
    return data;
}

The code runs, but Lambda returns [] as the result.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Ben Freke
  • 71
  • 1
  • 5
  • Potential duplicate of https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – horia Jun 13 '19 at 10:41
  • Wouldn't it be caused by `data` being _really_ an empty set, i.e. there's no data matching your query in DDB? – Stock Overflaw Jun 13 '19 at 11:59
  • No, if I run a .then() on the promise and console.log it, I get the response I'm expecting. – Ben Freke Jun 13 '19 at 12:01

2 Answers2

3

I figured it out! Numpty moment.

According to AWS, to get the results of a promise in Lambda you need to return the promise. So changing my code to:

const AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
const ddb = new AWS.DynamoDB();
const ddbGeo = require('dynamodb-geo');
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'MyGeoTable');
const myGeoTableManager = new ddbGeo.GeoDataManager(config);

exports.handler = function (event, context) {
    return myGeoTableManager.queryRadius({
        RadiusInMeter: 1000,
        CenterPoint: {latitude: 51.50, longitude: -0.17}
    });
};

Solved it.

Ben Freke
  • 71
  • 1
  • 5
  • 7
    Your original code was returning a promise though. Any function marked as `async` really returns a promise. The `awaits` obscure that a bit. Glad you have it working, but I think there is more to the story. – bigh_29 Jun 14 '19 at 18:53
0

Try as I might, I just cannot see an issue with your code. If .then(data => console.log(data) works, then your code really should work. Perhaps it would be more helpful for me to leave this question to someone who can see something that I cannot.. But in the meantime, you can probably get it to work using the callback style of lambda invocation. Maybe it'll even help reveal another detail of the issue...

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB();
const ddbGeo = require('dynamodb-geo');
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'MyGeoTable');
const myGeoTableManager = new ddbGeo.GeoDataManager(config);

exports.handler = function (event, context, callback) {
    myGeoTableManager.queryRadius({
        RadiusInMeter: 1000,
        CenterPoint: {latitude: 51.50, longitude: -0.17}
    })
        .then(data => {
            console.log(data);
            callback(null, data);
        });
}
jameslol
  • 1,829
  • 16
  • 18