serverless-express icon indicating copy to clipboard operation
serverless-express copied to clipboard

eventSource with many records

Open ivan-sirosh opened this issue 3 years ago • 4 comments

Could you please provide some example how to handle records n+1 size? For example I need to handle stream of events form SNS.

exports.handler = async (event, context) => {

  event.Records.forEach((record) => {
    console.log('Stream record: ', JSON.stringify(record, null, 2));
  });

// ???

}; 

ivan-sirosh avatar Mar 26 '21 08:03 ivan-sirosh

Hey, did you figure this out? Best way is probably to loop and process within your Express route.

brettstack avatar May 31 '21 06:05 brettstack

@brettstack Hello, It depends on requirement.

Simple solution it is just iterate over records. If you need to handle responses, need to introduce a temporal storage for requests then process as express does. Accordingly to requests return matched responses back to the lambda handler.

ivan-sirosh avatar May 31 '21 09:05 ivan-sirosh

If you want to do this, you'd need to wrap Serverless Express and do something like this:

const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

const se = serverlessExpress({ app })

exports.handler = async (event, context) {
  const httpResponses = event.Records.map((record) => {
    console.log('Stream record: ', JSON.stringify(record, null, 2));
    const wrappedRecord = wrapRecordInEventFormatWithRecordAsBody(record)
    const wrappedContext = maybeYouNeedToProvideRecordContextAlso(record)
    return se(wrappedRecord, wrappedContext)
  })

  const singleResponse = doSomethingWith(httpResponses)

  return singleResponse // make sure this returns a response structure your event source expects
}

brettstack avatar May 31 '21 10:05 brettstack

hello! @ivan-sirosh

Did you manage to solve this problem? I still have doubts on how to apply this solution.

Thank you

robertocerdasilva avatar Mar 09 '23 12:03 robertocerdasilva