Search code examples
nestjsserverlessserverless-framework

NestJS Serverless - App keeps reinitiating itself


I'm trying to setup a NestJS Serverless application. The app is starting fine with sls offline start. But the problem is that the app keeps reinitiating itself. The cache server is not set. Every time I call the endpoint, the server is reinitiated.

Lambda

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 { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import express from "express";

let cachedServer: Server;

async function bootstrapServer(): Promise<Server> {
    console.log("cached?", !!cachedServer);
    if (!cachedServer) {
        const expressApp = express();
        const nestApp = await NestFactory.create(AppModule, new ExpressAdapter(expressApp))
        nestApp.use(eventContext());
        await nestApp.init();
        
        // here is the set
        cachedServer = createServer(expressApp);
    }
    return cachedServer;
}

export const handler: Handler = async (event: any, context: Context) => {
    // here is another set
    cachedServer = await bootstrapServer();
    return proxy(cachedServer, event, context, 'PROMISE').promise;
}

Output

ANY /dev (λ: graphql)
cached? false
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [NestFactory] Starting Nest application... +186ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [InstanceLoader] AppModule dependencies initialized +3ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [RoutesResolver] AppController {/}: +0ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [NestApplication] Nest application successfully started +0ms
(λ: graphql) RequestId: cl3r0i9bu000b49v67zkk50gl  Duration: 12.56 ms  Billed Duration: 13 ms


ANY /dev (λ: graphql)
cached? false
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [NestFactory] Starting Nest application... +188ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [InstanceLoader] AppModule dependencies initialized +3ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [RoutesResolver] AppController {/}: +1ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [RouterExplorer] Mapped {/, GET} route +0ms
[Nest] 24777  - 05/29/2022, 9:00:31 AM     LOG [NestApplication] Nest application successfully started +0ms
(λ: graphql) RequestId: cl3r0i9h5000e49v65mfs21vo  Duration: 15.21 ms  Billed Duration: 16 ms

Does anyone have any ideas?


Solution

  • EDIT:

    --allowCache is now default behavior on >= 9.0.0

    Old Answer:

    From the docs: serverless-offline is a plugin that emulates lambda and api-gateway.

    Meaning not the execution environment, it will act as if you are calling a cold function everytime. You need to add an option to get it to work as a real function.

    sls offline start --allowCache