-
-
Save ryanfitz/8febe89a8208c810f98b to your computer and use it in GitHub Desktop.
| 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."); | |
| } | |
| }); | |
| }; |
Nice work @ryanfitz. Is there any library that maps the new image directly to an object I can populate to cloudsearch? I hate having to manually go into the field and access the .S or .N, it seems a bit mad when the whole point of dynamodb is to have dynamic fields.
Great job @ryanfitz. How did you set up the policies so your lambda was able to upload documents to CloudSearch?
@brianfoody you can iterate over the keys in image and use output from aws-sdk/lib/dynamodb/converter (const output = require('aws-sdk/lib/dynamodb/converter').output;) to do the rest of the type mapping all of the way down (nested). See https://github.com/aws/aws-sdk-js/blob/v2.32.0/lib/dynamodb/converter.js#L124-L176
Something like (using the newer lambda node versions):
const output = require('aws-sdk/lib/dynamodb/converter').output;
// ...
data.fields = Object.keys(image).reduce((prev, next) => {
prev[next] = output(image[next]);
return prev;
}, {});
@ryanfitz Only had to change my endpoint and field names. Thanks.