AWS Lambda function times out when I require aws-sdk module
Asked Answered
D

3

8

I'm trying to query a DynamoDB table from a Lambda function (for an Alexa skill), but when I send the intent that calls require('aws-sdk'), the skill seems to hang and timeout. The Alexa test page just says "There was a problem with the requested skill's response", and I don't see any errors in the CloudWatch logs. I have the skill set up to catch any errors and return them as a spoken response, so I'm sure it's not an uncaught exception. I've also tried wrapping the require in a try/catch block, but that didn't work either.

This is the module that gets loaded with require if the test database intent request is received:

const AWS = require('aws-sdk');

module.exports = () => {
  return 'Success!';
};

If I comment out require('aws-sdk'), the function works properly and Alexa responds with "Success".

Why does my skill break when all I'm doing is requiring the aws-sdk module?

I'm very new to AWS and this is my first experience trying to access a DynamoDB table in a Lambda function.

The Lambda function is uploaded as a zip that contains my source code, package.json (that includes aws-sdk as a dependency), and the node_modules folder.

Discard answered 18/4, 2018 at 19:43 Comment(2)
Have you found a solution to this since then? I'm having the exact same problem (i.e. my Lambda function times out while trying to require aws-sdk, no matter how large timeout I set for it; it doesn't always happen, but often enough to become a problem).Kristoferkristoffer
@DánielKis-Nagy I was able to fix it, but I honestly can't remember what I changed. I'm still using the aws-sdk module. I do remember that DynamoDB was causing problems because the initial connection would sometimes take so long the function would timeout.Discard
B
4

After hours of debugging, I've found that changing import * as AWS from 'aws-sdk'; to import {DynamoDB} from 'aws-sdk'; (or {CloudFront} or whatever you actually use) made the timeout issue disappear. Mind you, the time to actually connect to DynamoDB was never an issue for me, it was always the import line where the timeout happened.

Bydgoszcz answered 24/8, 2018 at 17:23 Comment(1)
Thanks for that. For NodeJS: const DynamoDB = require('aws-sdk/clients/dynamodb')Stillas
C
3

This can be fixed by either increasing the timeout or the memory allotted to the lambda function.

This is probably because the SDK is too big to be imported by the default timeout value of 3 seconds and the default memory value of 128 MB. This is why it will probably work if you try importing smaller components like only DynamoDB.

Connel answered 2/10, 2019 at 8:0 Comment(0)
G
1

Lambda, when using NodeJS, uses a callback continuation model. Your module should export a function that takes three parameters: event, context, and callback.

Event provides input parameters.

The other two are used for returning control from your handler function, depending on the NodeJS version you are using.

Try adding the three parameters that I mentioned and the, from within your exported handler function, call:

module.export = function(event, context, callback) {
   callback(‘success’);
}

Bear in mind, I wrote this on mobile off the top of my mind, so you may need to make small afjustments to the code but the idea is the same. Don’t return directly from the function, but call the callback to supply the response as a continuation. And note that in earlier versions of NodeJS, prior to version 4, you would have to use the context to set success or failure, rather than calling the callback.

For more details, see the Lambda with NodeJS tech docs on AWS.

The other thing to keep in mind is that for Alexa specifically, the response should be in the correct format. That is a JSON response that contains all the necessary elements as explained in the Alexa Skills Kit tech docs.

The Alexa ASK sdk that you’ve included generates those responses but I thought I should point you to the actual docs in case you were going to try building the response by hand to understand how it works.

Glycoprotein answered 19/4, 2018 at 0:8 Comment(1)
My skill code is setup correctly. The function above is called and the result is passed as callback(null, result). I've just got things setup so that I can return a string for an intent and it will be used as the speech response.Discard

© 2022 - 2024 — McMap. All rights reserved.