serverless-express
serverless-express copied to clipboard
eventSource with many records
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));
});
// ???
};
Hey, did you figure this out? Best way is probably to loop and process within your Express route.
@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.
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
}
hello! @ivan-sirosh
Did you manage to solve this problem? I still have doubts on how to apply this solution.
Thank you