Created
August 8, 2017 14:20
-
-
Save rodolfo42/7516a8a9b9b7ad402ab32d6a3fbbbe08 to your computer and use it in GitHub Desktop.
DynamoDB streams lambda trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const items = [{ | |
"Username": "First", | |
"Timestamp": "2017-08-07T19:51:00.794Z", | |
"Message": "first item in batch" | |
}, | |
{ | |
"Username": "Second", | |
"Timestamp": "2017-08-07T19:51:00.794Z", | |
"Message": "second item in batch" | |
}, | |
{ | |
"Username": "Third", | |
"Timestamp": "2017-08-07T19:51:00.794Z", | |
"Message": "third item in batch" | |
}]; | |
const putRequests = items.map((item) => { | |
return { | |
PutRequest: { | |
Item: item | |
} | |
}; | |
}); | |
const params = { | |
RequestItems: { | |
BarkTable: putRequests | |
} | |
}; | |
const aws = require('aws-sdk'); | |
const REGION = 'us-east-1'; | |
const db = new aws.DynamoDB.DocumentClient({ region: REGION }); | |
db.batchWrite(params).promise().then(() => { | |
console.log('success'); | |
}).catch((e) => console.error(e)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const TABLE_NAME = 'BarkTable'; | |
const REGION = 'us-east-1'; | |
exports.handler = (event, context, callback) => { | |
const DynamoDB = new AWS.DynamoDB.DocumentClient({region: REGION}) | |
const records = event.Records; | |
console.log('Processing ' + records.length + ' records'); | |
const jsonRep = JSON.stringify(records, null, 2); | |
console.log('Events: ' + jsonRep); | |
const lastRecord = records.pop(); | |
const inserts = records.map((record) => { | |
if (typeof record.dynamodb.NewImage === 'undefined') { | |
return Promise.resolve(); | |
} | |
const Username = record.dynamodb.NewImage.Username.S; | |
const Timestamp = record.dynamodb.NewImage.Timestamp.S; | |
const Message = record.dynamodb.NewImage.Message.S; | |
const Item = { Username, Timestamp, Message, Processed: "true" }; | |
const params = { | |
TableName: TABLE_NAME, | |
Item | |
} | |
return DynamoDB.put(params).promise(); | |
}); | |
Promise.all(inserts).then(() => { | |
callback('Failed to process last record: ' + JSON.stringify(lastRecord, null, 2)); | |
}).catch(err => { | |
callback(err); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Records": [ | |
{ | |
"eventName": "INSERT", | |
"dynamodb": { | |
"NewImage": { | |
"Message": { | |
"S": "Testing...1...2...3" | |
}, | |
"Timestamp": { | |
"S": "2016-11-18:14:32:17" | |
}, | |
"Username": { | |
"S": "Jane Doe" | |
} | |
} | |
} | |
}, | |
{ | |
"eventName": "INSERT", | |
"dynamodb": { | |
"NewImage": { | |
"Message": { | |
"S": "Testing...3...2...1" | |
}, | |
"Timestamp": { | |
"S": "2016-11-18:14:32:17" | |
}, | |
"Username": { | |
"S": "John Smith" | |
} | |
} | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment