how to avoid getting invocation error from lambda function being too idle

0

i am using lambda function in my react application through API gateway. When i open my lambda function it gets an error which is resolve by reloading and when i keep my page idle for too long and invoke my function it gets an error. I found that the instance allocated to my lambda function is gone if my page is idle. how do i resolve this error. I found that by allocating a certain reserved concurrency to it , it can be resolved but I am not sure

  • What is the exact error that you received? From where?

  • due to lambda cold start, request times out

1 Answer
1
Accepted Answer

I'm guessing you are getting a time-out because your cold starts to take too long. I think API gateway times out after 30 sec and this cant be changed, and the client (web browser) can configure this as well so some users might time out faster.

If you have this problem, you should optimize your cold start (https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/).

If you want to do a workaround here, you can use "Provisioned Concurrency", but I would not recommend it because it will not stop your end users from facing a cold start once in a while. Then you are probably better of running the app on AWS Fargate, then you can do pro active scaling.

Hope this helps!

//Carl

profile picture
answered 2 years ago
  • thank you for your answer, it was very helpful. It's a small-scale application so at max 10 users use the page. So, I was thinking of using setInterval on my frontend application that keeps calling the lambda at intervals(30 min) to keep it warm. Will that be a viable solution? and does the lambda get cold after a fixed time or is the timing unknown

  • We have no SLA for how long the lambda will stay worm, but I think 15 min is some kind of a "guideline". But your end users will still reach a cold start once in a while. But sure, before Provisioned Concurrency, many used this "keep warm strategy" your talking about. If you go beyond the lambda free tier, however, I don't think you will have much of a cost-benefit with this strategy over a small fargate container.

    If you are unable to optimize the startup time by fixing your code (30 sec is a super long time for a lambda), you can try to increase the lambda RAM, this will give it more CPU and network bandwidth, so it will start faster.

    If increasing the size doesn't do the trick you can have a look at the below factors:

    1. Runtime, with python, JS or Go you should be fine. If you use C#, you have a problem. If you are using Java, look into GraalVM and quarkus.
    2. Startup API calls, If you do a bunch of API calls during could starts, (get certificates, get files big form S3, populate local cache etc). Try to break this out, maybe use an EFS that can store data between executions, or implement some lacy loading.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions