0

I'm pretty new to AWS API Gateway, Lambda and DynamoDB, but I did a little bit of research and figured out how to build a simple Lambda function that scans a DynamoDB table.

I think I've successfully scanned the table and in the callback have access to the results within the data variable.

Right now, my function just completes with a Succeeded message, but I can't for the life of me actually figure out how to display the data that it scanned as the response. Any help would be greatly appreciated.

var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler = function(event, context) {
    var tableName = "MyTableName";
    dynamodb.scan({
        TableName : tableName
    }, function(err, data) {
        if (err) {
            context.done('error','reading dynamodb failed: '+err);
        }
        context.succeed('Success');
    });
};
2
  • Do you mean how to stream the data? You can return the data as JSON. Example callback(null, jsonString); Commented Nov 1, 2016 at 14:10
  • @notionquest Yes, return the JSON data. Can you expand on where that code would actually be placed within the function? Commented Nov 1, 2016 at 14:14

2 Answers 2

1

Right now you are returning the string 'Success'. Instead, return whatever you actually want to return. For example: context.succeed(null, data);

Sign up to request clarification or add additional context in comments.

1 Comment

That did it, thank you. I thought I tried that already, and it gave an error Process exited before completing request, but this time it worked, so I appreciate it!
0

Please refer the sample code here.

exports.handler = function(event, context, callback) {
    var tableName = "MyTableName";
    dynamodb.scan({
        TableName : tableName
    }, function(err, data) {
        if (err) {
            context.done('error','reading dynamodb failed: '+err);
        }            
        callback (null, data);
    });
};

Comments

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.