0

I have a lambda function that's suppose to be writing to a database. When I run it on my local machine it works but then when I upload it to lambda and test it It doesn't put anything in the database. The role I have the function using has full access to DynamoDB and its the exact same code that works fine when I run it from my laptop. Any idea why that would be the case?

Here's my lambda. The dao class contains the code that actually accesses dynamo. I'm just trying to upload some constant strings right now.

const DAO = require('./PostStatusDAO.js');

exports.handler = async (event, context, callback) => {
    var dao = new DAO();
    dao.post("this is a test", "@jordan", "@matt", "none");       
    const response = {
        statusCode: 200,
        body: {
            result: "good"
        }
    };
    return response;
};
const AWS = require('aws-sdk');

const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});

class PostStatusDAO {
    post(in_text, in_user, in_author, in_attachment) {
        var params = {
            Item: {
                user: String(in_user),
                timestamp: Date.now(),
                author: String(in_author),
                text: String(in_text),
                attachment: String(in_attachment),
            },

            TableName: 'Feed',
        };

        console.log(params);

        var result = ddb.put(params, (err, data) => {
            console.log("callback");
            if(err) {
                console.log("Error: ", err);
            } else {
                console.log("Data: ", data);
            }
        });

        // console.log(result);
    }
}

module.exports = PostStatusDAO;
2
  • You should wait for the ddb.put operation to be finished before returning the response from lambda. Commented Nov 16, 2019 at 4:28
  • What is showing in CloudWatch Logs? Commented Nov 16, 2019 at 6:28

1 Answer 1

1

To see the reason why your function is failing you have to either run it synchronously or return the promise back to the caller/runtime like this:

const DAO = require('./PostStatusDAO.js');

exports.handler = async(event, context, callback) => {
    var dao = new DAO();

    // Return new promise 
    return new Promise(function(resolve, reject) {
     // Do async job
        dao.post("this is a test", "@jordan", "@matt", "none", function(err, data) {
            if (err) {
                console.log("Error: ", err);
                reject(err);
            }
            else {
                console.log("Data: ", data);
                resolve(data);
            }
        })
    })
};

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

const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});

class PostStatusDAO {
    async post(in_text, in_user, in_author, in_attachment, callback) {
        var params = {
            Item: {
                user: String(in_user),
                timestamp: Date.now(),
                author: String(in_author),
                text: String(in_text),
                attachment: String(in_attachment),
            },

            TableName: 'Feed',
        };

        console.log(params);

        return ddb.put(params, callback).promise();
    }
}

module.exports = PostStatusDAO;
Sign up to request clarification or add additional context in comments.

2 Comments

May I ask WHY we need to run synchronously, or use a async/wait pattern? When the ddb.put(...) method is called, the underlying infrastructure should just proceed to write the result, no? Not sure if this is a javascript thing, or something special about lambda and/or AWS infra
All requests made through the SDK are asynchronous and use a callback interface. It's just the way AWS designed it. Lambda support both, async and non-async handlers, it's up to you, see more here: docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

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.