0

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);
user2233677
  • 199
  • 1
  • 8

3 Answers3

0

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.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • If you want to add more code to your question please edit your question to add it there so it is readable. Don't put a bunch of code int a comment. – Mark B Aug 17 '23 at 16:38
0

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)
jarmod
  • 71,565
  • 16
  • 115
  • 122
0

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!

user2233677
  • 199
  • 1
  • 8
  • 2
    The correct way to interact with Stack Overflow is to accept the answer that solved your problem, rather than to write an answer stating largely the same thing. Please delete this answer and accept the earlier provided answer. – jarmod Aug 18 '23 at 15:38