question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cold Start Trick with new v4 module

See original GitHub issue

I saw this interesting medium post based on aws-serverless-express that helps with cold-start in nestjs by moving the bootstrap part to the init stage and not within the handler, like shown below image

Need help with translating this to the new v4 way of bootstrapping the express server using nestjs as the proxy is deprecated.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

1reaction
Muthuveerappanvcommented, Aug 12, 2021

@Muthuveerappanv Did you figure it out?

Nope 😒

0reactions
samjmarshallcommented, Mar 30, 2022

Hey @Muthuveerappanv, after trying a few different implementations, I’ve managed to get a working updated solution for my services:

import { APIGatewayEvent, Callback, Context, Handler } from 'aws-lambda';

import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core';
import serverlessExpress from '@vendia/serverless-express';

let server: Handler;

async function bootstrap(): Promise<Handler> {
  const nestApp = await NestFactory.create(AppModule);
  await nestApp.init();
  const app = nestApp.getHttpAdapter().getInstance();
  return serverlessExpress({ app });
}

bootstrap().then((handler) => (server = handler));

function waitForServer(
  event: any,
  context: any,
  callback: Callback,
): Promise<any> {
  return new Promise((resolve) =>
    setImmediate(async () => {
      if (!server) {
        resolve(await waitForServer(event, context, callback));
      } else {
        resolve(server(event, context, callback));
      }
    }),
  );
}

export const handler: Handler = async (
  event: APIGatewayEvent,
  context: Context,
  callback: Callback,
): Promise<any> => {
  if (server) return server(event, context, callback);
  return await waitForServer(event, context, callback);
};

This seems to be working fine for me at the moment. But occasionally I’ll see an initial DB connection error in the logs on startup, although it always immediately retries and succeeds.

ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...`

To be honest the error seems to have little impact, when it occurs. I’m guessing this occasional startup error could be caused by the container network services simply not being ready in time. 🤷

Read more comments on GitHub >

github_iconTop Results From Across the Web

Priming for a Cold Start - PilotWorkshops
Starting an airplane during cold conditions is often more of an art than procedure. Know your POH recommendations, pre-heat if possible, and don't...
Read more >
Cold start of Azure Function with large npm module trees is ...
Hi--, I've notice that the initial call of an Azure function (i.e. a couple of minutes after the last call) takes a significant...
Read more >
ATV Won't Start When Cold (This is why) - ATVFixed.com
Cold Starting an ATV; Fuel Injected Cold Start; Faulty Battery ... rely on the plug you have removed as a reference unless you...
Read more >
What's the trick to cold starting? - Megasquirt Support Forum ...
The first factor is having the fuel dialed in correctly when it's warmed up. With the exception of cranking pulse width, all other...
Read more >
4-STROKE STARTING TIPS - Dirt Bike Magazine
Quick tricks from the Javeline. Cold starting: Yamaha says to twist the throttle twice to prime the carb, but I pull in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found