Last active
May 10, 2024 16:49
-
-
Save ugurgungezerler/f8047d9dfb29abc8791d4e756b4c5eb2 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