Python – AWS Lambda Function State (zappa)

AWS Lambda Function State (zappa)… here is a solution to the problem.

AWS Lambda Function State (zappa)

I want to optimize the flask application lambda server by adding an internal cache to change relatively slow data (for example, a website dropdown might change several times a year). I use zappa to deploy to Lambda. Does this make sense? Or whether it is flashed each time a request is processed. I know I can’t rely on aws that saves state, and my goal is to optimize performance slightly without spending a fortune on some Redis instances, let alone ElastiCache.

Update: Yes, serverless deployment frameworks like zappa recycle state, so why shouldn’t I do this. The following hackernoon blog discusses state recycling in more detail

https://hackernoon.com/write-recursive-aws-lambda-functions-the-right-way-4a4b5ae633b6

Whilst Lambda functions are ephemeral by design, containers are still
reused for optimization which means you can still leverage in-memory
states that are persisted through invocations.

Not sure if such a cache can be invalidated, the env variable could be a lambda instance, http,
SNS can be difficult/expensive.

Solution

Yes, this does not apply to Lambda.

You must use some kind of third-party cache.


If caching your GET requests alone is enough for you, you can use a CDN for this.

I personally use CloudFlare CDN to cache all GET requests for n minutes. You can receive a lot of requests for free.
You simply define a custom Page Rule to cache all content for a particular URL pattern.

CloudFlare Page Rule

Of course, the same thing can be done with CloudFront (staying in the AWS ecosystem) or most other CDNs.

Related Problems and Solutions