Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Modify the default AWS Lambda function timeout

    Tomasz ŁakomyTomasz Łakomy

    Our AWS Lambda functions may take some time to execute, especially when they are going to call external APIs etc.

    The default timeout is 3 seconds, which may not be enough.

    In this lesson we're going to learn how to modify the default timeout for an AWS Lambda function as well as how to use the context object provided with each lambda call in order to see how much time is remaining before a function timeouts.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: 0:00 We have a Lambda function calling an external API. In this case, it's calling the Cat Fact API and is going to print a random cat fact. If I test this function, I'm going to see the cat fact over here.

    0:13 This Lambda function also has a sleep function in it. The sleep function is going to delay our function by the timeout provided as an argument. If I were to call it like this, I'm going to delay this execution of this GET request by 5 seconds. Let's see what happens. I'm going to save it, and I'm going to test it.

    0:33 Right now, after 5 seconds, I'm going to get a timeout, because our task has timed out after 3 seconds. Let's see what we can do about it. If we scroll down in the AWS console, what we're going to see is that there is a timeout in memory in basic settings of this Lambda function.

    0:49 By default, every Lambda function is going to time out after 3 seconds. This can be increased. For instance, I can set it to 30 seconds. The maximum timeout, as of today, is 15 minutes. You could also increase the memory for your function. You can put it up to close to 3 gigabytes, but I'm going to leave it as 128 megabytes, because this is more than enough for this function.

    1:12 After I save that and go over here, and test it again, we're going to see that there will be no longer a timeout. In fact, I am delaying this GET request by 5 seconds. We can see the random cat fact over here.

    1:24 We might wonder how much time do we have left in order to execute our code before the timeout hits. In order to do that, we have to use the context object. Every time a Lambda function gets called, it receives an event and also a context as an argument. Context is providing an information about this current function invocation.

    1:42 The thing that we're going to use from the context is that we are going to console.log context, get remaining time in milliseconds. If I go ahead and console.log that and save it, let me test that. After this function has finished executing, we can see over here that we have close to 25 seconds remaining in order to execute this function.

    2:00 This makes quite a lot of sense, because we are delaying this function by 5 seconds, and we also need some time in order to process the GET request. We also have 25 seconds remaining before we hit the 30-second timeout.