0

I am pretty new to node and facing a problem while trying to do a simple test on AWS lambda-dynamo DB integration in order to get a response for Amazon Lex request. If someone can say what needs to be changed that would be much appreciated. thanks..

Runtime - Node js 10.x and also tried on node js 8.10.. Below is the node JS sample code :

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

var DBHandler = require("./DBHandler")

exports.handler = async (event) => {

    console.log('This event is' +JSON.stringify(event))

    var intent = event.currentIntent.name;

    DBHandler.getalldetails(intent , function (err , data ) {
        if (err) {
            context.fail(err);
        } else {
            var response = {
            "dialogAction": {
            "type": "Close",
            "fulfillmentState": "Fulfilled",
            "message": {
                "contentType": "PlainText",
                "content": "data.Item.Message."
            } 
          }
        }

    return response
    //callback ( null, response );
        }
    });
};

Below is the ./DBHandler in another file under the same lamdba function folder.

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

AWS.config.update({
    region:"eu-west"
});

var docClient = new AWS.DynamoDB.DocumentClient();

var tableName = "testholly";
//exports.handler = (event,context,callback) => {

var getalldetails = (Intent,callback) => {

    var params = {
        TableName : tableName,
        Key: {
            "Intent":Intent
        }
    };
    docClient.get(params,function (err,data) {
        callback (err , data);
    });
};module.exports = {
    getalldetails
};

2 Answers 2

1

First check the Dynamo DB access permissions to that lambada. If at all not given, create a role to access the dynamoDB table and assign it to the lambda function.

If you want to access the dynamodb without role then use cognito pool ID or AWS access key Secret access key in AWS.config();

Sample:

AWS.config.update({
  accessKeyId: "",
  secretAccessKey: "",
  region: "us-east-1"
});

OR

 AWS.config.update({ 
          "region":"us-east-1"
    });
 AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
      IdentityPoolId:"Your identity_pool_id"
   });
Sign up to request clarification or add additional context in comments.

Comments

0

your response is not within your callback from getAllDetails()....it should be. So something like:

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

    console.log('This event is' + JSON.stringify(event))

    var intent = event.currentIntent.name;

    DBHandler.getalldetails(intent, function (err, data) {
        if (err) {
            context.fail(err);
        } else {
            var response = {
                "dialogAction": {
                    "type": "Close",
                    "fulfillmentState": "Fulfilled",
                    "message": {
                        "contentType": "PlainText",
                        "content": data.Item.Message
                    }
                }
            }
            callback(null, response)
        }
    });
};

You cannot use await w/ callbacks, you would need to "promisify" that bad boy. In the above, I pass the callback to the handler.

3 Comments

Hi LostJon - i get a syntax error if i move callback to pass to the handler. Parsing error - unexpected token callback
Thanks again LostJon - i had tried that option already, no errors but null value from Dynamo DB...
can you confirm that your accessing code actually retrieves data? if not, that can be your issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.