Red Baron
Red Baron

Reputation: 7682

Why is my lambda function not working as expected?

I'm getting this error from lambda: errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: Unable to stringify response body

I'm trying to trigger my lambda from an upload in s3 (this part is working) and then that should post something into dynamoDB

here is my lambda in full

var AWS = require('aws-sdk');
var S3 = require('aws-sdk/clients/s3');

const s3 = new AWS.S3()
var DynamoDB = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {
    // TODO implement
    var bucket = event['Records'][0]['s3']['bucket']['name']
    var json_file_name = event['Records'][0]['s3']['object']['key']
    var params = {
      Bucket: bucket, 
      Key: json_file_name
     };

    const data = await s3.getObject(params).promise();

    const dataToDb = data.Body.toString('utf-8');

    console.log(dataToDb, 'TESTING ====')

    var dbparams = {
        TableName: "MY-TABLE-NAME",
        Item: dataToDb, 
    };

    const putIntoDB = await DynamoDB.put(dbparams, function (err) {
         if (err) {
             console.log(err, 'er===');
         }
        else {
            console.log(dbparams, 'db====')
        }
    });

    return putIntoDB
};

the data that is coming from s3 is just a simple json object with 4 keys so nothing big at all. my table consists of just a primary key called user_id and that field IS in my json object that comes back from S3 so no idea why this isn't working?

Upvotes: 4

Views: 7026

Answers (2)

Thales Minussi
Thales Minussi

Reputation: 7245

DynamoDB.put is a callback and you are trying to await on it, so this will never work. You should chain the .promise() method just like you did with your s3.getObject call.

await DynamoDB.put(dbparams).promise()

If you want to handle errors, just wrap it in a try/catch block:

try {
    await DynamoDB.put(dbparams).promise()
} catch (e) {
    console.log(e)
}

On top of that, you also need to feed the Item attribute of your DynamoDB call with a JSON object, not with a string.

I am not sure what you get from s3, but converting it to a JSON object by using JSON.parse(dataToDb) should do the trick given the data is a valid JSON object.

Upvotes: 8

EddieDean
EddieDean

Reputation: 1816

UTF-8 encoding is usually utf8 when used as an argument in node. I would start there.

Upvotes: 0

Related Questions