Created
June 12, 2022 19:04
-
-
Save djheru/8b00cf095b648d8a442d6ddd83e7de10 to your computer and use it in GitHub Desktop.
Nestjs Application as Lambda Function
This file contains 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 { NestFactory } from '@nestjs/core'; | |
import { ExpressAdapter } from '@nestjs/platform-express'; | |
import serverlessExpress from '@vendia/serverless-express'; | |
import { Context, Handler } from 'aws-lambda'; | |
import express from 'express'; | |
import { AppModule } from './app.module'; | |
let cachedServer: Handler; | |
async function bootstrap() { | |
if (!cachedServer) { | |
const expressApp = express(); | |
const nestApp = await NestFactory.create(AppModule, new ExpressAdapter(expressApp)); | |
nestApp.enableCors(); | |
await nestApp.init(); | |
cachedServer = serverlessExpress({ app: expressApp }); | |
} | |
return cachedServer; | |
} | |
export const handler = async (event: any, context: Context, callback: any) => { | |
const server = await bootstrap(); | |
return server(event, context, callback); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment