Home > Net >  How to query DynamoDB table from NodeJS?
How to query DynamoDB table from NodeJS?

Time:01-06

I have Lambda function that needs to query a table in DynamoDB.

The DynamoDB table was created with the following format :

              params = { 
                TableName: TableName,
                Item: {
                  'id' : {'S': someid},
                  'value' : {'S': 'somevalue'},
                  'requestfiltereddate' : {'N' : String(FilteredDate)},
                  'uri':{'S': uri},
                  'ip':{'S': ip},
                  'useragent':{'S': userAgent},
                }
              };
              
              await ddb.putItem(params).promise();

The table has a index on the column requestfiltereddate named : requestfiltereddate-index.

then I'm trying to query the same table with the following code :

              params = {
                    TableName: 'MyTable',
                    IndexName: "requestfiltereddate-index",
                    KeyConditionExpression: "requestfiltereddate = :date1",
                    ExpressionAttributeValues: {":date1": {"N": String(Date.now())}},
                    ProjectionExpression: "id"
                };                                            
              
               var results = await ddb.query(params).promise();

When testing the function, I'm not getting any answer. Insteam I'm getting a generic error with no meaning or details to investigate :

    Response
    {
      "errorMessage": "2023-01-06T01:48:52.156Z 555e0a58-e021-4ae2-b431-xxxxxxx Task timed out after 1.05 seconds"
    }

    Function Logs
    START RequestId: 555e0a58-e021-4ae2-b431-xxxxxxxxxxxxx Version: $LATEST
    2023-01-06T01:48:52.156Z 555e0a58-e021-4ae2-b431-xxxxxxxxxxxxx Task timed out after 1.05 seconds

    END RequestId: 555e0a58-e021-4ae2-b431-xxxxxxxxxxxxx
    REPORT RequestId: 555e0a58-e021-4ae2-b431-xxxxxxxxxxxxx Duration: 1054.53 ms    Billed Duration: 1000 ms    Memory Size: 128 MB Max Memory Used: 26 MB

    Request ID
    555e0a58-e021-4ae2-b431-xxxxxxxxxxxxx

does anyone know what I'm doing wrong please ?

Thanks. Cheers,

CodePudding user response:

The error message that you're getting shows that your Lambda function reached its timeout (after 1.05s) and was terminated before it finished running.

You can try increasing the function timeout in the AWS Lambda Console to a larger value to give the Lambda function more time to finish running (https://docs.aws.amazon.com/lambda/latest/dg/configuration-function-common.html#configuration-timeout-console).

CodePudding user response:

There are 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.

To troubleshoot the retry and timeout issues, first, to allow enough time for a response to the API call, add time to the Lambda function timeout setting. Then review the logs of the API call to find the problem. Next, change the retry count and timeout settings of the AWS SDK as needed for each use case.

https://docs.aws.amazon.com/lambda/latest/dg/resource-model.html

https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html

  • Related