0 0
WP Ad Inserter plugin
Read Time:5 Minute, 38 Second

What  is AWS Lambda

Lambda is a computing service provided through Amazon Web Services (AWS, for short). Lambda is “serverless,” giving developers the freedom to focus on running applications from the cloud rather than maintaining physical servers. 

With over 200 unique services, AWS is the world’s most comprehensive and broadly adopted cloud platform. Lambda supports various languages such as Node.js, Python, Ruby, Java, and several others. 

As one of the key services provided by AWS, Lambda only charges by time computing, being a cost-effective option depending on the service needed. 

A Closer Look at Lambda Timeout

In AWS Lambda, there are three different timeouts a user can run into. Timeouts can occur from Event sources (such as the AWS API gateway), services (this can be other third-party apps), and Lambda functions. In this article, we will focus on the lambda function timeout.

For every function, there is a set amount of time that it can be run before the timeout kicks in. The timeout limit can be adjusted starting from 1 second to the max limit of 15 minutes. This allows the freedom to allocate the proper computing resources (and, thus, financial resources) to a given function based on its complexity.

Example error log where the API call failed to establish a connection (connection timeout)

START RequestId: b81e56a9-90e0-11e8-bfa8-b9f44c99e76d Version: $LATEST

2018-07-26T14:32:27.393Z    b81e56a9-90e0-11e8-bfa8-b9f44c99e76d    [AWS ec2 undefined 40.29s 3 retries] describeInstances({})

2018-07-26T14:32:27.393Z    b81e56a9-90e0-11e8-bfa8-b9f44c99e76d    { TimeoutError: Socket timed out without establishing a connection

Example error log where the API call connection was successful but timed out after the API response took too long (socket timeout)

START RequestId: 3c0523f4-9650-11e8-bd98-0df3c5cf9bd8 Version: $LATEST

2018-08-02T12:33:18.958Z    3c0523f4-9650-11e8-bd98-0df3c5cf9bd8    [AWS ec2 undefined 30.596s 3 retries] describeInstances({})

2018-08-02T12:33:18.978Z    3c0523f4-9650-11e8-bd98-0df3c5cf9bd8    { TimeoutError: Connection timed out after 30s

 

Why the AWS Lambda timeout Happens

Three reasons why retry and timeout issues occur when invoking a Lambda function with an AWS SDK:

  • A remote API is unreachable or takes too long to respond to an API call.
  • The API call doesn’t get a response within the socket timeout.
  • The API call doesn’t get a response within the Lambda function’s timeout period.

Ensure Function Idempotence

As mentioned in the AWS Lambda documentation, one key method to avoid inconsistencies or lost data is to verify that your function is idempotent.

Idempotence is a characteristic of an operation, such as an HTTP endpoint call. It allows one to execute it twice in a short time interval and observe the same result as if the operation was only applied once.

How to Avoid Lambda Timeout

Reconfigure Functions

If you find that the timeout limit is not enough time to perform the function as needed, it could be that you need to re-evaluate the function.

CloudWatch

CloudWatch provides Duration metrics that tell us how much time a Lambda function takes throughout a particular period. It also tells us the Average Duration, which can be used to baseline the function timeout limit.

Cloudwatch

However, CloudWatch doesn’t tell us how much time each downstream call takes. This information is critical to setting a timeout limit for integrated services.

Amazon X-Ray

Amazon X-Ray can help you discover the timeout value for downstream services. X-Ray shows the execution time taken by all downstream systems.

In the example below, it shows the execution time of S3 GET (171ms), and S3 PUT (178ms) requests.

Amazon x ray

Optimize Function Memory

Another component that can be adjusted per function in AWS Lambda is the amount of memory used. The range of memory allowed per function is between 128 MB and 10,240 MB. For memory, the system will default to the lowest setting, which will often be sufficient.

To set the timeout for the current running function, we can use this code:

var server = app.listen();

server.setTimeout(6000);

And to set the timeout for each API call, we can use this code:

app.post(‘/xxx’, function (req, res) {

  req.setTimeout( context.getRemainingTimeInMills() – 500 ); // 500ms to account recovery steps

});

Other Best Practices To Handle Lambda Timeout Errors

  1. Use short timeout limits for event sources – set timeout to 3-6 seconds for API calls.  For Kinesis, DynamoDB Streams, or SQS, you should adjust the limits based on the batch size.
  2. Monitor Lambda function timeouts – put monitoring in place using CloudWatch and X-Ray and fine-tune the timeout values as applicable.
  3. Use fallback methods – if timeouts are unavoidable, either return the response with an error code and description or use fallback methods. Fallback methods can use cached data or get data from an alternative source (check out Hystrix or Spring Retry for Java or oibackoff lib for Node.js)
  4. Avoid exceeding DynamoDB write capacity – if you increase concurrent executions for your function, avoid exceeding the 40,000 writes limit. Use node-rate-limiter in Node.js.
  5. Optimize your function – if your function is running longer than the desired timeout value, check the steps that the function is performing. If you are doing too many things in one function, consider Step Functions to break it into smaller pieces.
  6. Balance performance and cost – to increase performance, Lambda gives only a single option – increase memory. More memory equals more CPU. There are a few common scenarios:
  • If a function logic is CPU intensive, add memory to reduce the execution time. It not only saves costs but also reduces timeout errors.
  • If a function spends most of its time on DB operations, there is no point in increasing memory. It won’t help.
  • Add memory to bring execution time below the nearest 100 – AWS charges for Lambda usage in increments of 100ms. For example, if the average execution is 110ms, increase memory to bring it below 100ms, or you’ll be charged for 200ms.

Conclusion

In short, we looked at what AWS Lambda is and what methods can be implemented to mitigate its timeout limitations. In cases when we have simple Lambda functions, and we would like only to expand the scope of the Lambda function, going for a lift and ship solution, taking advantage of CodeBuild for example, would be more suitable. 

For cases when we have more complex Lambda functions, and we would like to expand the scope and the features of the Lambda functions, choosing to decouple the application layer would be more suitable.

Source: https://blog.educationecosystem.com/how-to-resolve-the-aws-lambda-timeout/

Making a Real-Time Clock With a Conic Gradient Face Previous post Making a Real-Time Clock With a Conic Gradient Face
How to log Correlation IDs in .NET APIs with Serilog Next post How to log Correlation IDs in .NET APIs with Serilog

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.