Reputation: 81
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.
Upvotes: 4
Views: 6017
Reputation: 81
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.
Upvotes: 4
Reputation: 1935
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);
});
}
Upvotes: -1