How to handle AWS Lambda timeout issues

Have you ever wondered how you can handle a timeout in AWS lambda? The serverless miracle doesn't provide a way to do it ideally. But there are ways to manage the scenario if it occurs. Sounds good? Let's deep dive into it.

Kuharan Bhowmik
2 min readNov 26, 2022

--

https://unsplash.com/photos/FV6Pma5U_98

Table of Contents —

AWS Lambda
Why We Use It
Context
The problem
The solution

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers.

Why We Use It —

There are multiple solid reasons why we use AWS Lambda.

  1. It shoulders the availability and scalability problems on our behalf of us.
  2. Great integration with other AWS products like SQS, SNS, Kinesis, or API gateway.
  3. Out-of-the-box logging features with AWS CloudWatch.
  4. It costs less in most cases since it only charges you for execution time.

Context —

The timeout occurs when the function exceeds the max value of 15 mins! That is the current limit.

As per AWS —

“You can now configure your AWS Lambda functions to run up to 15 minutes per execution. Previously, the maximum execution time (timeout) for a Lambda function was 5 minutes.

You can now set the timeout value for a function to any value up to 15 minutes. When the specified timeout is reached, AWS Lambda terminates execution of your Lambda function.”

The problem —

What if your function execution doesn't complete in 15 mins and it shouts — "errorMessage": "2024-11-16T07:06:45.969Z b66dd037-ba01-4025-97db-6522b446eac8 Task timed out after 900 seconds"

Here, the program execution has ended saying the task timed out after 15 mins as it was not able to complete the process fully.

The solution —

Luckily, there is a context object which gives us the remaining time in milliseconds. By using this I would be able to know how much time is left before my lambda would die!

 context.get_remaining_time_in_millis()

The signal.signal() the function allows defining custom handlers to be executed when a signal is received. A handler for a particular signal, once set, remains installed until it is explicitly reset.

Below is an example of the framework that can be used to handle the timeout.

import signal

def timeout_handler(_signal, _frame):
global unprocess_bucket
global unprocess_file

logger.info("Time exceeded! Creating Unprocessed File.")

session = boto3.Session()
s3_client = session.client(service_name="s3")
s3_client.put_object(
Body="",
Bucket=unprocess_bucket,
Key=unprocess_file.replace(
unprocess_file.split("/")[1], "doc_pdf/unprocessed_files"
),
)


signal.signal(signal.SIGALRM, timeout_handler)


def lambda_handler(event, context):
signal.alarm(int(context.get_remaining_time_in_millis() / 1000) - 15)
## rest of the code ##

The AWS lambda function timeout_handler() code will get executed according to the time you set in the alarm function. Now you can write your Plan B here and get it done.

Thank you for reading. I hope this helps. You can follow me here for more such articles. Cheers :)

--

--