-
-
Save saifkhan192/83836ac2466750e4688b83abe4b711dd to your computer and use it in GitHub Desktop.
NestJS SQS Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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