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);
2
Answers
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):Note, the high-level document client is still available in the v3 SDK.
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 noItem
property.So, one of the following is the likely cause: