Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JongSeokJang/e72c56eb201d261831d0e5336d0c805b to your computer and use it in GitHub Desktop.
Save JongSeokJang/e72c56eb201d261831d0e5336d0c805b to your computer and use it in GitHub Desktop.
index dynamodb data to cloudsearch using AWS Lambda
var AWS = require('aws-sdk');
exports.handler = function(event, context) {
var cloudsearchdomain = new AWS.CloudSearchDomain({endpoint: 'doc-dev-cinch-accounts-ltmqj5gt5mjb5hg5eyqaf2v5hu.us-east-1.cloudsearch.amazonaws.com'});
var documents = event.Records.map(function(record) {
var data = {id : record.dynamodb.Keys.id.S};
if (record.eventName === 'REMOVE') {
data.type = 'delete'
} else {
var image = record.dynamodb.NewImage;
data.type = 'add'
data.fields = {
name : image.name.S,
username : image.username.S,
email : image.email.S
};
}
return data;
});
var params = {contentType: 'application/json', documents : JSON.stringify(documents) };
console.log('uploading documents to cloudsearch domain', params);
cloudsearchdomain.uploadDocuments(params, function(err, data) {
if(err) {
console.log('Error uploading documents to cloudsearch', err, err.stack);
context.fail(err);
} else {
context.succeed("Successfully processed " + event.Records.length + " records.");
}
});
};
@JongSeokJang
Copy link
Author

"message": "{ ["The first non-whitespace character in the file must be '['"] }",
"code": "DocumentServiceException",
"time": "2016-04-26T10:56:18.858Z",
"requestId": "814bp1d66-0basd-11e6-b7fc-b9b1ad0761693",
"statusCode": 400,
"retryable": false,
"retryDelay": 17.98068769276142

@JongSeokJang
Copy link
Author

the error appears because CloudSearch expects the message to be an array of objects, but instead you were sending just one object.

@JongSeokJang
Copy link
Author

@JongSeokJang
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment