How do I avoid cold starts? Why do I see function calls to my lambda when nobody accessed it? What is keep_warm in zappa? How much does it cost me?
Cold starts happen when your function hasn’t been used for a while (the time is defined by AWS and, from experience, varies per region and types of lambdas). A cold start means that the AWS lambda service is not just running your function but actually setting up all the infrastructure needed for it to run, which can add some seconds to your the execution time. If that causes a timeout to trigger, it can cause a non-reproducible error on your responses (the first request times out, but the second works because it reuses the infrastructure created by the first request).
For low usage lambdas, a workaround is the
keep_warm functionality of zappa. By default it’s configured to
true, so you have it enabled by default. The option schedules a request every 4 minutes to your lambda to try to keep the infrastructure for your lambda up. The request is treated automatically by the zappa handler so none of your code is triggered. It’s not a perfect heuristic, but it works for some people.
If you have constant traffic, keep_warm won’t do anything for you. Turning it off makes sense.
If your region/type of lambda is getting cooled down faster so that the 4 minutes is too slow, you might need to also configure the rate. If you want the rate to go down to one minute, set
"keep_warm_expression": "rate(1 minutes)", in your
The cost of the default lambda config on a lambda with 512MB of RAM (also zappa’s default) is approximately $0.02/month if you’ve exhausted the free tier.