Pretty much what the title says:
Running sls offline start against a particular lambda function that stores a variable in memory to be reused for subsequent requests, it seems a new process is created for every call instead of reusing existing instances of a function as Lambda does. Here's my example, which works in AWS, but not using serverless-offline:
import { Context } from 'aws-lambda';
import * as serverless from 'aws-serverless-express';
import { NestFactory } from '@nestjs/core';
import { ApiModule } from './module';
const express = require('express')();
let served;
const createServer = () =>
NestFactory.create(ApiModule, express)
.then(async app => {
//app.setGlobalPrefix('api');
app.init();
})
.then(() => serverless.createServer(express));
export const handler = (event: any, context: Context) => {
if (served) {
console.log('Loading existing NestJS server...');
return serverless.proxy(served, event, context);
} else {
console.log('Creating NestJS server...');
createServer().then(server => {
served = server;
return serverless.proxy(served, event, context);
});
}
};
This spins up an Express server (using NestJS), stores it in the served var and checks for its existence on subsequent calls, so it doesn't have to recreate it each time.
On AWS, all n+1 calls within the lifespan of a function log Loading existing NestJS server..., but using serverless-offline, I get Creating NestJS server... every single time.
Is this expected?
If so, is there a way to circumvent this behavior?
If not, any ideas how we might fix this?
@jonnyasmar I'm experiencing the same. Did you figure out a workaround?
Unfortunately, nothing yet @Glavin001
I'm focused on another area of my project, atm - so I haven't dug into it much. But tbh, I have no idea where to begin troubleshooting this.
This appears to have fixed the problem for me! 馃
- serverless offline start
+ serverless offline start --skipCacheInvalidation
Yes skipCacheInvalidation is the correct reponse to this issue. Thanks @Glavin001
--allowCache Worked for me.
current sls --version:
Framework Core: 1.72.0
Plugin: 3.6.13
SDK: 2.3.1
Components: 2.30.12
skipCacheInvalidation does nothing.
Most helpful comment
This appears to have fixed the problem for me! 馃