aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.04k stars 569 forks source link

QueryCommand: failed to query index #6429

Closed Luan10017 closed 4 days ago

Luan10017 commented 1 week ago

Checkboxes for prior research

Describe the bug

QueryCommand receiving timeout when trying to query index (GSI), however when observing the index reading data in the table console there is no reading processing. Furthermore, the same logic and data work perfectly with the dynamodb SDK V2 and nodejs 14 lambda runtime.

SDK version number

@aws-sdk/lib-dynamodb

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

Nodejs 20

Reproduction Steps

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";

const client = new DynamoDBClient({});
const dynamo = DynamoDBDocumentClient.from(client);

export const handler = async (event, context) => {
    if (process.env.DEBUG) {
      console.log({
        message: "Received event",
        data: JSON.stringify(event, null, 2),
      });
      console.log({
        message: "Received contex",
        data: JSON.stringify(context, null, 2),
      });
    }

    const firstDate = event?.firstDate;
    const secondDate = event?.secondDate;
    const emailToResponsible = event?.emailToResponsible;
    const ExclusiveStartKey = event?.ExclusiveStartKey;

    try {

          const emailData = await getDataByEmail(
            emailToResponsible,
            firstDate,
            secondDate,
            ExclusiveStartKey
          );
          return response(200, emailData);

    } catch (error) {
      console.error(JSON.stringify(error, null, 2));
      return response(500, error);
    }
  };

async function getDataByEmail(
    emailToResponsible,
    firstDate,
    secondDate,
    ExclusiveStartKey
  ) {
    const params = {
      TableName: tableName,
      IndexName: "emailToResponsible",
      KeyConditionExpression:
        "emailToResponsible = :emailToResponsible_prefix AND #dateField BETWEEN :date1 and :date2",
      ExpressionAttributeValues: {
        ":emailToResponsible_prefix": emailToResponsible,
        ":date1": firstDate,
        ":date2": secondDate,
      },
      ExpressionAttributeNames: {
        "#dateField": "createdAt",
      },
    };

    if (ExclusiveStartKey) {
      params.ExclusiveStartKey = ExclusiveStartKey;
    }

    const items = await dynamo.send(new QueryCommand(params));

    return items;
  }

  function response(status, body) {
    return {
      statusCode: status,
      body: body,
      headers: {
        "Content-Type": "application/json",
      },
    };
  }

Observed Behavior

QueryCommad receives a timeout error after 30 seconds, but no read attempts are recorded in the table index reading metrics. Furthermore, using the AWS SDK V2, the query returns in 400 ms.

Expected Behavior

Data return in time compared to SDK V2 400 ms.

Possible Solution

No response

Additional Information/Context

No response

aBurmeseDev commented 1 week ago

Hi @Luan10017 - thanks for reaching out.

Furthermore, the same logic and data work perfectly with the dynamodb SDK V2 and nodejs 14 lambda runtime.

Node.js 14 runtime for Lambda was deprecated on Dec 4, 2023, and AWS SDK for Javascript v2 is entering Maintenance mode on Sept 8, 2025.

Can you confirm if you're running this on supported Lambda runtime?

Here are a few steps you can take to further investigate:

Luan10017 commented 4 days ago

Hi @aBurmeseDev thanks for the support.

I performed the debug step with middlewareStack as you advised, but what really solved my problem was deleting the lambda and recreating it again. I think there was a problem with the provisioning of the resource (remembering that this was in lambda node 20 runtime).

Thanks again for helping! I'm closing the issue.