Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS-SDK Load Error in AWS Lambda function using NodeJS

I am creating new lambda functions using nodejs. And this lambda functions works well without aws-lambda. But when I require 'aws-sdk' package, it occurs the error and stop running. The error is that they can't require the 'aws-sdk' package. But aws-sdk was already in node_module folder.

I want you to solve this problem. Many thanks.

like image 374
Mikhail Zharnikov Avatar asked Dec 10 '22 08:12

Mikhail Zharnikov


2 Answers

It's very interesting and strange question.

I have also experienced this issue too. At first, when I faced this problem, I was very worried and it looked really very strange. And it took me days and days of time to solve this problem.

The reason is really very simple. You meet that problem due to the timeout of lambda function.

The default timeout is 3 seconds and 3 seconds is too short time to load aws-sdk package.

To load aws-sdk package, it needs at least 6 seconds. So I recommend you to set the timeout more than 6 seconds whenever you want to use aws-sdk function.

like image 115
Derek Wang Avatar answered Dec 12 '22 22:12

Derek Wang


If this function runs thousands of times a day for 5 seconds or so then it can get quite costly. If your lambda is currently waiting around for another task to finish before completing execution then it would be better to consider a messaging system e.g. SNS.

I have a lambda function which requires aws-sdk, and then updates DynamoDB and upon the completion of that request invokes another lambda function and I've never seen all of these going above 1 seconds. If you are calling another lambda function make sure to include InvocationType: 'Event' so the original lambda completes right away instead of waiting around for the second lambda function to finish.

If that still doesn't work then it's time to try SNS as described here

like image 44
Adi H Avatar answered Dec 12 '22 22:12

Adi H