Skip to content

Instantly share code, notes, and snippets.

@nason
Created March 20, 2019 20:46
Show Gist options
  • Save nason/3f9ca2c1626d0da7882fc09cf7af6ac4 to your computer and use it in GitHub Desktop.
Save nason/3f9ca2c1626d0da7882fc09cf7af6ac4 to your computer and use it in GitHub Desktop.

Revisions

  1. nason created this gist Mar 20, 2019.
    29 changes: 29 additions & 0 deletions base-lambda-stream-trigger-handler.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    async function handleInsert(record) {}
    async function handleUpdate(record) {}
    async function handleRemove(record) {}

    async function receiveHandler(event, context, callback) {
    const results = await Promise.all(
    event.Records.map(record => {
    console.log(JSON.stringify(record));
    switch (record.eventName) {
    case 'INSERT':
    return handleInsert(record);
    case 'MODIFY':
    return handleUpdate(record);
    case 'REMOVE':
    return handleRemove(record);
    default:
    console.error('Unknown Event');
    console.log(JSON.stringify(record));
    // Return, not throw, so stream processing continues
    // If an error is thrown from this handler, the event will be retried
    return `Unknown event: ${record.eventName}`;
    }
    })
    );

    console.log(results);

    return `Successfully processed ${event.Records.length} records.`;
    }