AWS SDK v3 GetCommand API fails to find an item in DynamoDB database

1.2k Views Asked by At

I have a Node.js Lambda function that uses AWS DocumentClient to find an item by primary key in a DynamoDB table. This worked fine in AWS SDK v2 and returned the item data. The item does exist in the database.

var params = {
      TableName : TBL_ICT_DEVICES,
      Key: {
        'imei': imei
      }
    };
    _docClient.get(params,function(err,data){
    ...

When I switched to AWS SDK using GetCommand the item is not found
Here is the v3 code I use

const getCommand = new GetCommand({
        TableName: TBL_ICT_DEVICES,
        Key: {
           imei: imei
        },
         ConsistentRead: true,
    });
    console.log("command = " + JSON.stringify(getCommand));
    const getResponse = await _docClient.send(getCommand);

Here are the relevant lines from the execution log

2023-08-17T14:10:19.908Z    613a7a50-3c3e-4eee-8f76-5614b97d1631    INFO    command = {"middlewareStack":{},"input":{"TableName":"ict_devices","Key":{"imei":"865284042591606"},"ConsistentRead":true},"inputKeyNodes":[{"key":"Key"}],"outputKeyNodes":[{"key":"Item"}],"clientCommand":{"middlewareStack":{},"input":{"TableName":"ict_devices","Key":{"imei":"865284042591606"},"ConsistentRead":true}}}
2023-08-17T14:10:20.155Z    613a7a50-3c3e-4eee-8f76-5614b97d1631    INFO    Query error : {"name":"ResourceNotFoundException","$fault":"client","$metadata":{"httpStatusCode":400,"requestId":"QBA35I5NBBHMOS3IVNGQIGQ01BVV4KQNSO5AEMVJF66Q9ASUAAJG","attempts":1,"totalRetryDelay":0},"__type":"com.amazonaws.dynamodb.v20120810#ResourceNotFoundException"}

What am I missing? Thank you
Andy

Adding type information did not work
Tried both DynamoDB and DocumentClient
The client objects are declared as follows

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; 
import { DynamoDBDocumentClient, GetCommand,PutCommand } from "@aws-sdk/lib-dynamodb"; 
const _ddbClient = new DynamoDBClient({}); 
const _docClient = DynamoDBDocumentClient.from(_ddbClient);
3

There are 3 best solutions below

1
Mark B On

It appears that you went from using the high-level DynamoDB document client in the SDK v2, to the low-level DynamoDB client in SDK v3.

The low-level client expects keys with type information, like this (assuming imei is a string value):

        Key: {
           imei: {
               S: imei
           }
        },

Note, the high-level document client is still available in the v3 SDK.

0
jarmod On

The ResourceNotFoundException error means that the requested table was not found. It does not mean that the requested item was not found. If the item was not found, you would get a normal 200 response but it would have no Item property.

So, one of the following is the likely cause:

  1. you specified the wrong region (where the specified table does not exist)
  2. you specified the wrong table name (so the table doesn't exist)
  3. you used the wrong AWS credentials (so you're querying the wrong AWS account)
1
user2233677 On

That was it ! The region was not specified. Once I added it in the DynamoDbClient constructor the getCommand returned the result I expected.
Observation

  • Specifying the key type produced an error The provided key element does not match the schema. Without one worked with both DocClient and DDBClient

This is the one that worked

new GetCommand({
        TableName: TBL_ICT_DEVICES,
        Key: {
            'imei': imei
        },
         ConsistentRead: true,
    });

Thank you so much for help!