JavaEar 专注于收集分享传播有价值的技术资料

Task timed out after 3.00 seconds - Lambda application with nodeJS

I am trying to put a hard-coded data item to DynamoDB. I am using AWS SDK object to perform this update. And all the debug "Console.log" in the below code is getting printed but eventually it prints Task timed out after 3.00 seconds

With no update to the DynamoDB

function updatedb(intent, session, callback) {
    let country;
     const repromptText = null;
     const sessionAttributes = {};
     let shouldEndSession = false;

console.log("In the function");
const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-1' });

var params = {
    TableName: "Location",
    Item: {
        "LocationID": { "S": "11" },
        "Country": { "S": "10" },
        "Description": { "S": "10" },
        "Name": { "S": "10" }
    }
};
console.log("Param loaded & executing the DocClient Put");

docClient.put(params, function (err, data) {
    if (err) {
        speechOutput = 'Update failed';
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
        callback(sessionAttributes,
            buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
        speechOutput = 'Update successful';
        callback(sessionAttributes,
            buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
    }
});
}

The following items are already checked

1) There is a table named "Location" in DynamoDB 2) Both DynamoDB and this lambda function are in ue-west-1 (Ireland) 3) The role assigned for this Lambda function can do all operation on this table. See the policy details below

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "Stmt1510603004000",
        "Effect": "Allow",
        "Action": [
            "dynamodb:*"
        ],
        "Resource": [
            "arn:aws:dynamodb:eu-west-1:752546663632:table/Location"
        ]
    }
]
}

How does my Lambda function locate the table "location" just with the region?- the code does not appear to have end-point, etc.? - just developed based on a tutorial.

Is that what I am missing?

Please can you help?

1个回答

    最佳答案
  1. I believe that AWS locates the table based on your identity, in combination with the region and the table name.

    I was able to successfully post to a table using this code:

    const AWS = require('aws-sdk');
    
    const dynamoDB = new AWS.DynamoDB({region: 'us-west-2'});
    
    var params = {
      TableName: "my-table",
      Item: {
        "LocationID": { S: "11" },
        "Country": { S: "10" },
        "Description": { S: "10" },
        "Name": { S: "10" }
      }
    };
    
    dynamoDB.putItem(params, (err, data) => {
      if (err){
        console.error(err.stack);
      } else {
        console.log(data);
      }
    });
    

    If you can in fact post to the table from the CLI, then there is still at least one remaining issue: it appears that you are using the DocumentClient class incorrectly. It looks like you're mixing up the syntax for DynamoDB.putItem with the syntax for DynamoDB.DocumentClient.put.

    If you notice, my code uses the DynamoDB class directly-- based on what you're doing, I see no reason why you couldn't do the same. Otherwise, you should change your Item object:

    var params = {
      TableName: "my-table",
      Item: {
        "LocationID": "11",
        "Country": "10",
        "Description": "10",
        "Name": "10"
      }
    };
    

    My guess is your code is currently erroring out because you are trying to insert Maps where you want to insert Strings. If you have Cloudwatch configured you could check the logs.

    Finally, I don't see you using callback in your code. If your intention is to respond to a client calling the lambda you should do that. Depending on your NodeJS version, the lambda can simply time out without returning a useful response.