Daniel Brown
Daniel Brown

Reputation: 1164

How to keep state in AWS lambdas?

We have a library that gives us access to some external API, but it has a very long initialisation time. Also there is no way to initialize it with some session key or anything that could be stored, it always has to go through the whole initialisation process.

Is there a way to have the library initialized only once and then used across all lambda functions?

If we were somehow able to inject the session by reverse engineering the code, where would the session data be stored best? In DynamoDB, stage variables, something else?

Upvotes: 1

Views: 4897

Answers (1)

amittn
amittn

Reputation: 2355

  • There are global variables in aws lambda, any variable declared outside of lambda_handler handler can be used in the consecutive invocation provided the same container is used by aws. Not sure if that's going to be ok with your case. Since once the container is destroyed by aws then the next invocation done after that will have to again go through a re-initialization. This will all depend upon how often is this service going to be used or how busy its going to be.
  • If you are thinking of reverse engineering the code then in that case you have multiple options to store the session like DynamoDB, Amazon ElastiCache. Then it all depends upon your service needs and budget.
  • As suggested by elRull if you are able to build a service around it and keep it running in a t2.micro that might work. Like initialize it on the first call and then keep it alive and let the other invocations use it.

Upvotes: 3

Related Questions