aws-lambda#DynamoDBStreamEvent TypeScript Examples

The following examples show how to use aws-lambda#DynamoDBStreamEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: dynamo-kinesis-adaptor.ts    From ddd-cqrs-es-aws-sam with MIT License 6 votes vote down vote up
handler = async (event: DynamoDBStreamEvent, context: Context): Promise<void> => {
  const kinesis = new AWS.Kinesis();

  if (!_applicationContainer) {
    _applicationContainer = ApplicationContainer.instance();
  }

  _applicationContainer.logger.debug('Dynamo Kinesis Adaptor Event: %o', event);

  // push each dynamo stream EventItem record to a Kinesis Stream Event record
  const records = event.Records.filter(record => record.eventName === 'INSERT' && record.eventSource === 'aws:dynamodb').map(record => {
    const eventItem: EventItem = AWS.DynamoDB.Converter.unmarshall(record.dynamodb.NewImage) as EventItem;

    return {
      Data: JSON.stringify(eventItem.event),
      PartitionKey: record.dynamodb.Keys.id.S,
      SequenceNumberForOrdering: record.dynamodb.Keys.$version.N,
      StreamName: process.env.eventSourceStream
    };
  });

  // push each kinesis record in order
  for (const record of records)  {
    const result = await kinesis.putRecord(record).promise();
    _applicationContainer.logger.debug('Inserted Kinesis record: %o\n\rResult: %o', record, result);
  }
}