<img alt="AWS Lambda" data- data-src="https://kirelos.com/wp-content/uploads/2023/09/echo/AWS-Lambda.jpg/w=800" data- decoding="async" height="420" src="data:image/svg xml,” width=”800″>

If you want to build a serverless architecture in AWS, or at least a part of that, then the AWS Lambda service will be the most important part of that.

It is a serverless computing function (typically written in Node.JS or Python programming language) that you can develop and run without starting up any backend clusters or servers. You can connect multiple AWS Lambda functions to form more complex processes.

However, an important property of the Lambda function is its limitation in terms of processing time duration. This ultimately defines when it is the right purpose to use the AWS Lambda function.

<img alt="AWS-Lambda-RealTimeFileProcessing" data- data-src="https://kirelos.com/wp-content/uploads/2023/09/echo/AWS-Lambda-RealTimeFileProcessing.png/w=951" data- decoding="async" height="276" src="data:image/svg xml,” width=”951″>
Source: aws.amazon.com

What is the Timeout Interval?

The timeout feature of the AWS Lambda function is a setting that defines the maximum amount of time that a function can run before it is terminated.

Lambda functions are designed to be short-lived and stateless, and this is how they should be used. It isn’t a rare exception to see how teams try to utilize the Lambda function for long-running processes. They do that just because they want to leverage the benefits of the serverless nature of the AWS Lambda. It is always much cheaper to run a code without the necessity of starting up a server with a specific CPU and memory configuration. So, the intent is easy to understand.

However, if a function runs too long, it can cause resource utilization and performance issues. You risk the resources locking, and since you have the benefit of a long-running process, most likely, you don’t really care about the optimization of the steps inside the processes. Suddenly, a waiting time of five minutes is not much of an issue. You can afford to leave it present even if, for the process itself, it might be useless.

By default, the timeout for a Lambda function is set to just 3 seconds. In that case, you would need to count on the fact that whatever code you execute inside a Lambda function must finish within three seconds. This is great if you want to build an architecture with really snappy communications and response times, potentially with millions of transactions in a very short time. But that would limit the usable use cases of Lambda function quite significantly, so you can increase this limit up to a maximum of 900 seconds (15 minutes). When this timeout is reached, Lambda will terminate the function and return an error code.

How to Set up the Timeout Feature?

<img alt="lambda-large-function-memory" data- data-src="https://kirelos.com/wp-content/uploads/2023/09/echo/lambda-large-function-memory.png/w=1024" data- decoding="async" height="443" src="data:image/svg xml,” width=”1024″>
Source: aws.amazon.com

You can set the timeout for a Lambda function using the AWS Management Console, the AWS CLI, or the AWS SDKs.

Here are the concrete steps how to achieve this:

  1. Open the AWS Management Console and go to the Lambda service.
  2. Select the Lambda function that you want to modify.
  3. In the “Configuration” tab, scroll down to the “General Configuration” section and locate the “Timeout” setting.
  4. Click the “Edit” button next to it.
  5. Enter the new timeout value in seconds (between 1 and 900) and click the “Save” button.
  6. Click the “Save” button at the top of the page to save the changes to the Lambda function.

You can also use the AWS CLI command line to update the timeout of a Lambda function. Here’s an example command:

aws lambda update-function-configuration --function-name [My_Lambda_Function_Name] --timeout 900

This command sets the timeout for the [My_Lambda_Function_Name] to 15 minutes. Just put there your real name of the function and the desired timeout value in seconds.

Some Considerations

The timeout setting applies to the entire function, not just individual code blocks or operations within the function. So, if your function is performing a long-running operation, such as processing a large file or making a network request, you’ll need to ensure that the timeout is still enough for the operation to complete, even in its worst-case scenario.

It’s worth noting that Lambda functions are designed to be short-lived and stateless. They are intended to perform small, discrete tasks. If you find that your Lambda function is taking a long time to complete, it may be good to consider breaking it up into several smaller functions. You can then call them one by one by a trigger. For example, if the output of the previous Lambda function is stored on a specific S3 bucket.

Additionally, if you need to run a long-running process that exceeds the 15-minute limit, you can consider using AWS Step Functions to orchestrate a series of Lambda functions or other AWS services to achieve your desired outcome.

Best Practices

The working interval of the timeout itself does not seem to be large enough for some extensive talks. Most of the time, developers end up setting it up directly to 15 minutes for all Lambda functions. Even if the function will end up much sooner, there is no negative side effect of that.

Nevertheless, there are still some best practices to note.

#1. Define Appropriate Value

You should set the timeout value based on the expected execution time of the function. This basically means to set a value that is not too short but also not unnecessarily long.

You might want to make sure the overall time of a single Lambda function is not more than three minutes, for example, just because of the architectural or overall performance reasons of your application.

Similarly, you might want to make sure the Lambda function will not terminate sooner than, let’s say, three minutes. One of the reasons could be you want to fill in some number of retries inside the Lambda code. For example, in case some of the resources needed to complete the code are busy or locked.

So before you go ahead and set up the timeout to 15 minutes, give it some thought about what could be the optimal value of execution time. Defining more precise values is just another tool to give some boundaries to the whole development team.

#2. Monitoring Over Time

Once you set the timeout limits, make an effort to monitor the execution time of your Lambda functions. This will give you important insights about whether the original decision was right or needs some adjustments.

You can use CloudWatch metrics and logs to track the execution time of the functions. Then, identify those that are taking longer than expected as well as those that are much faster to complete than expected.

#3. Asynchronous Invocation

If your Lambda function is triggered by an event that does not require an immediate response, for example, a file upload or a message from a queue, use asynchronous invocation to reduce the risk of timeouts. This will make the function run in the background without waiting for a response. This can, of course, lead to a reduced amount of timeouts. This is simply because you don’t need to include in the overall execution time of the Lambda the waiting time that the function needs for that resource.

#4. Use Step Functions for Complex Processes

If you need to run a long-running process that exceeds the 15-minute limit, you can use AWS Step Functions to orchestrate a series of Lambda functions or other AWS services to achieve that outcome. Step Function will break up the process into smaller, more manageable tasks that you can execute within the timeout limit.

You can even run several lambda functions in parallel within a step function. Then, let the step function wait for all the parallel lambda functions before proceeding further. This is a form of horizontal scaling where several lambda functions can divide one problem and solve it partially together.

In the end, you just need to collect the partial results and build up the final resolution, for which one following the standalone lambda function can suffice.

#5. Optimize the Code

You can optimize your Lambda function code to reduce the execution time and improve performance. This is useful if the timeout interval is not sufficient only occasionally. In that case, it might be worth searching for code optimization to remove such failure cases.

How Does Timeout Impact on Billing?

<img alt="AWS-Lambda-Billing" data- data-src="https://kirelos.com/wp-content/uploads/2023/09/echo/AWS-Lambda-Billing.png/w=800" data- decoding="async" src="data:image/svg xml," width="800">
Source: aws.amazon.com

The timeout setting for an AWS Lambda function does not directly impact the billing for the function. AWS Lambda bills based on the number of requests and the duration of the function’s execution time.

If you increase the timeout setting for a Lambda function, it may result in longer execution times. But only if the function really needs that additional time. If you combine that with a higher frequency of a function that takes longer to execute, then yes, you might expect higher costs.

However, if the function is not invoked frequently or if it completes the process sooner than the timeout value is reached, the impact on billing may be minimal.

This is why setting up all Lambda functions to a timeout of 15 minutes by default might not be the main reason for the increase in the cost of your overall processing.

However, it certainly leads to an indirect increase in overall costs. Developers will feel they have some reserve to use, and they might not optimize the code to such an extent they would probably do if the timeouts were more restrictive but still realistic.

Another thing is that AWS Lambda provides a free tier that includes 1 million free requests and 400,000 GB-seconds of compute time per month. If your Lambda function usage falls within the free tier limits, you won’t pay for the function’s execution time regardless of the timeout setting.

Final Words

AWS Lambda function is a powerful tool, especially for serverless processing in the AWS cloud. It has its purpose and limits that we should be aware of. Let’s not try to use it for use cases which it was not designed for.

It’s perfect for simple, asynchronous, and ideally triggered actions that do not require execution time longer than 15 minutes. If you need anything more complex, use AWS Step Functions to combine several Lambda functions into a single orchestrated process. Or give up on cheaper serverless processing and use servers with appropriate configuration and computing power to accomplish your task.

Next, check out the introduction to AWS Lambda for beginners.