J. Cake
J. Cake

Reputation: 351

Lambda function timed out when trying to write data to DynamoDB

I´m trying to write a lambda function to insert a new User into my DynamoDB table Users

The table has UserId as index

¿Why this code doesn´t work?

'use strict'
const AWS = require('aws-sdk');
const uuid = require('uuid')
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-2'});

exports.handler = function(event, context, callback) {
    
    const params = {
        User: {
            "UserId":uuid.v1(),
            "username":event.username
        },
        TableName: process.env.TABLE_NAME
    }
    docClient.put(params, function(err,data){
        callback(err, data)
    })
  
};

My lambda test is simply:

{
  "username": "Javierito"
}

This test fails returning:

Task timed out after 3.00 seconds

Upvotes: 1

Views: 1315

Answers (1)

jarmod
jarmod

Reputation: 78663

The primary problem here is that the uuid module cannot be loaded. It's not available in the default Lambda runtime environment. The failure to load this module takes more than 3 seconds and hence your Lambda function times out (because you are using the 3 second default). You can include additional modules like uuid in your deployment upload, or use Lambda Layers.

If you increase the Lambda timeout to 30 seconds, you will see it fail with:

"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'uuid'"

Also, unrelated, your params object is incorrect. The property within it should be named Item, not User.

Upvotes: 2

Related Questions