import { Handler , Context } from 'aws-lambda' ; import { Server } from 'http' ; import { createServer , proxy } from 'aws-serverless-express' ; import { eventContext } from 'aws-serverless-express/middleware' ; import express from 'express' ; import { NestFactory } from '@nestjs/core' ; import { ExpressAdapter } from '@nestjs/platform-express' ; import { AppModule } from './app.module' ; const binaryMimeTypes = [ ] ; let cachedServer ; async function bootstrapServer ( ) { if ( ! cachedServer ) { const expressApp = express ( ) ; const nestApp = await NestFactory . create ( AppModule , new ExpressAdapter ( expressApp ) ) ; nestApp . use ( eventContext ( ) ) ; nestApp . enableCors ( { origin : true , } ) ; await nestApp . init ( ) ; cachedServer = createServer ( expressApp , undefined , binaryMimeTypes ) ; } return cachedServer ; } export const handler = async ( event , context ) => { cachedServer = await bootstrapServer ( ) ; return proxy ( cachedServer , event , context , 'PROMISE' ) . promise ; } ;