I have an API Gateway integrated with Lambda. I’m trying to do something inside ‘processStuff’ route and then notify users once the processing is done. The problem is the notification (PostToConnection) only happens at the next lambda invocation. How do I make sure that PostToConnection is done before lambda finishes executing
export const handler = async (event) => {
const { connectionId, routeKey } = event.requestContext;
try {
switch (routeKey) {
case "$connect":
return await handleConnect(connectionId);
case "similarInterestsMatchmaking":
return await processStuff(JSON.parse(event.body), connectionId);
case "$disconnect":
return await handleDisconnect(connectionId);
default:
return { statusCode: 500, body: 'Unknown Error.' };
}
} catch (e) {
if (e instanceof HandlerError) {
const errorCommand = new PostToConnectionCommand({
ConnectionId: connectionId,
Data: Buffer.from(JSON.stringify({ type: "error", message: e.message }))
});
await apigw.send(errorCommand);
return { statusCode: 500, body: 'Unknown Error.' };
}
throw e;
}
};
async function processStuff (body, connectionId) {
// do something here
const matchNotificationCommand = new PostToConnectionCommand({
ConnectionId: connectionId,
});
await apigw.send(matchNotificationCommand);
}
processStuff route’s Lambda proxy integration is already set to true.