Serverless-offline: Global variables are not frozen

Created on 21 Nov 2019  路  5Comments  路  Source: dherault/serverless-offline

Sometimes, you want to freeze "global" variables to maintain state between executions. For example (from [1]), until the lambda is not killed, then counter gives a different value at every invocation.

let counter = 0

module.exports.handler = (event, context, callback) => {
  counter++
  console.log(counter)
  callback(null, { count: counter })
}

Even if we usually assume that lambda are stateless and we should not rely on this freezing process for data storage, there are cases when keeping a temporary state in this way is useful (see [1]).

I think that serverless-offline is not able to emulate this behavior.

[1] https://www.jeremydaly.com/reuse-database-connections-aws-lambda/

question

Most helpful comment

@dnalborczyk it works! I'll close this issue, since it is resolved. Thanks a lot.

What about making this solution more visible in README?

Thanks for the great work!

All 5 comments

hey @mstn which version of this plugin are you using? are you using any flags?

Tomorrow, I will try to reproduce the problem with a minimal project. Sorry, I should have done it before posting here.

Version

 "serverless-offline": "^5.12.0",

Serverless version 1.55.1.

Invoked with sls offline start --config serverless.yml

I am using it with other plugins

plugins:
  - [email protected]
  - [email protected]
  - [email protected]

thanks @mstn ! no need for a repro, I think I know what you are experiencing.

just copy pasting from my comment where your issue is related: https://github.com/dherault/serverless-offline/pull/820#issuecomment-536736770

If I remember correctly, v5 should also do what you expect. Unfortunately, there is an [somewhat] unknown caveat: you likely have to use the skipCacheInvalidation option, in order to turn off handler reloading during development. this feature caused a lot of issues and has been removed from v6, meaning, what you expect should be the default in v6 now.

Let me know if setting this option helps, more here: https://github.com/dherault/serverless-offline/tree/v5.x-release#usage-and-command-line-options

As an alternative, you could also try the latest v6 alpha, without this option.

let me know if this helps.

@dnalborczyk it works! I'll close this issue, since it is resolved. Thanks a lot.

What about making this solution more visible in README?

Thanks for the great work!

@dnalborczyk hi, it is me again. Is it possible that with skipCacheInvalidation hot reloading stops working?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yareyaredesuyo picture yareyaredesuyo  路  4Comments

adambiggs picture adambiggs  路  4Comments

Dong9769 picture Dong9769  路  4Comments

ktwbc picture ktwbc  路  4Comments

FranzSkuffka picture FranzSkuffka  路  3Comments