logesh
logesh

Reputation: 303

AWS Lambda time out in 6 seconds

I am using the serverless framework with nodejs(Version 4.4) to create AWS lambda functions. The default timeout is 6 seconds for lambda execution. I am connecting to mysql database using sequelize ORM. I see errors like execution timed out. Sometimes my code works properly even with this error. But sometimes nothing works after this timeout error. Its really hard for me make sense out of this timeout. I am afraid increasing the timeout will incur more charge.

Upvotes: 1

Views: 1574

Answers (1)

Udo Held
Udo Held

Reputation: 12548

If you are seeing errors like 'execution timed out' than you are probably cutting the execution of your Lambdas with a too low timeout.

There might be several reasons for this:

  • The initialization of the container can be slow, this should only occur for the first call of container. If you have a low memory setting and load lots of libraries it can happen that it takes quite a while(usually this shouldn't be a problem with node)
  • Connecting to a database can be slow
  • If you reuse database connections, it's possible that they are stale and this can lead to a timeout.
  • Your database queries may be slow.

To mitigate the problem you should temporarily add some logging to your Lambda and increase the timeout, so that you can figure out what actually takes so long. Unless you are already a heavy Lambda user you are unlikely to use up your 400.000 free GB-seconds a month. If you run your Lambdas with 128 MB this equates to 3.200.000 seconds per month / 103.225 seconds per day / 28.5 hours per day. Try to test with higher memory settings as well, depending on case this can even reduce the total GB/s consumed.

As others pointed out already you only pay for the time actually used, so if your Lambda finishes faster than the timeout you only pay for the actual time consumed(in 100 ms increments).

Upvotes: 2

Related Questions