Skip to content

Instantly share code, notes, and snippets.

@ugurgungezerler
Last active May 10, 2024 16:49
Show Gist options
  • Save ugurgungezerler/f8047d9dfb29abc8791d4e756b4c5eb2 to your computer and use it in GitHub Desktop.
Save ugurgungezerler/f8047d9dfb29abc8791d4e756b4c5eb2 to your computer and use it in GitHub Desktop.
NestJS SQS Lambda
import { Handler, Context } from 'aws-lambda';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { INestApplication } from '@nestjs/common';
import { SqsHandlerService } from './queue/sqs.handler.service';
let cachedServer: INestApplication;
async function bootstrapServer(): Promise<INestApplication> {
if (!cachedServer) {
const nestApp = await NestFactory.create(AppModule);
await nestApp.init();
cachedServer = nestApp;
}
return cachedServer;
}
export const handler: Handler = async (event: any) => {
cachedServer = await bootstrapServer();
const handler = cachedServer.get(SqsHandlerService);
const message = event.Records[0]; // this can be batch
const job = JSON.parse(message.body);
await handler.handleSqsEvent(job);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment