Skip to content

Instantly share code, notes, and snippets.

@djheru
Created June 12, 2022 19:04
Show Gist options
  • Save djheru/8b00cf095b648d8a442d6ddd83e7de10 to your computer and use it in GitHub Desktop.
Save djheru/8b00cf095b648d8a442d6ddd83e7de10 to your computer and use it in GitHub Desktop.
Nestjs Application as Lambda Function
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